update asterisk to new upstream release (v1.2.1)
[oweals/openwrt.git] / openwrt / target / linux / linux-2.6 / patches / generic / 104-pf_ring.patch
1 diff --unified --recursive --new-file linux-2.6.12.5/include/linux/ring.h linux-2.6.12.5-1-686-smp-ring3/include/linux/ring.h
2 --- linux-2.6.12.5/include/linux/ring.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.12.5-1-686-smp-ring3/include/linux/ring.h 2005-10-22 23:50:44.951445250 +0200
4 @@ -0,0 +1,108 @@
5 +/*
6 + * Definitions for packet ring
7 + *
8 + * 2004 - Luca Deri <deri@ntop.org>
9 + */
10 +#ifndef __RING_H
11 +#define __RING_H
12 +
13 +
14 +#define INCLUDE_MAC_INFO
15 +
16 +#ifdef INCLUDE_MAC_INFO
17 +#define SKB_DISPLACEMENT    14 /* Include MAC address information */
18 +#else
19 +#define SKB_DISPLACEMENT    0  /* Do NOT include MAC address information */
20 +#endif
21 +
22 +#define RING_MAGIC
23 +#define RING_MAGIC_VALUE      0x88
24 +#define RING_FLOWSLOT_VERSION    5
25 +#define RING_VERSION          "3.0"
26 +
27 +#define SO_ADD_TO_CLUSTER        99
28 +#define SO_REMOVE_FROM_CLUSTER  100
29 +#define SO_SET_REFLECTOR        101
30 +
31 +/* *********************************** */
32 +
33 +#ifndef HAVE_PCAP
34 +struct pcap_pkthdr {
35 +  struct timeval ts;    /* time stamp */
36 +  u_int32_t caplen;     /* length of portion present */
37 +  u_int32_t len;        /* length this packet (off wire) */
38 +};
39 +#endif
40 +
41 +/* *********************************** */
42 +
43 +enum cluster_type {
44 +  cluster_per_flow = 0,
45 +  cluster_round_robin
46 +};
47 +
48 +/* *********************************** */
49 +
50 +#define RING_MIN_SLOT_SIZE    (60+sizeof(struct pcap_pkthdr))
51 +#define RING_MAX_SLOT_SIZE    (1514+sizeof(struct pcap_pkthdr))
52 +
53 +/* *********************************** */
54 +
55 +typedef struct flowSlotInfo {
56 +  u_int16_t version, sample_rate;
57 +  u_int32_t tot_slots, slot_len, tot_mem;
58 +  
59 +  u_int64_t tot_pkts, tot_lost;
60 +  u_int64_t tot_insert, tot_read;  
61 +  u_int16_t insert_idx;
62 +  u_int16_t remove_idx;
63 +} FlowSlotInfo;
64 +
65 +/* *********************************** */
66 +
67 +typedef struct flowSlot {
68 +#ifdef RING_MAGIC
69 +  u_char     magic;      /* It must alwasy be zero */
70 +#endif
71 +  u_char     slot_state; /* 0=empty, 1=full   */
72 +  u_char     bucket;     /* bucket[bucketLen] */
73 +} FlowSlot;
74 +
75 +/* *********************************** */
76 +
77 +#ifdef __KERNEL__ 
78 +
79 +FlowSlotInfo* getRingPtr(void);
80 +int allocateRing(char *deviceName, u_int numSlots,
81 +                u_int bucketLen, u_int sampleRate);
82 +unsigned int pollRing(struct file *fp, struct poll_table_struct * wait);
83 +void deallocateRing(void);
84 +
85 +/* ************************* */
86 +
87 +typedef int (*handle_ring_skb)(struct sk_buff *skb,
88 +                              u_char recv_packet, u_char real_skb);
89 +extern handle_ring_skb get_skb_ring_handler(void);
90 +extern void set_skb_ring_handler(handle_ring_skb the_handler);
91 +extern void do_skb_ring_handler(struct sk_buff *skb,
92 +                               u_char recv_packet, u_char real_skb);
93 +
94 +typedef int (*handle_ring_buffer)(struct net_device *dev, 
95 +                                    char *data, int len);
96 +extern handle_ring_buffer get_buffer_ring_handler(void);
97 +extern void set_buffer_ring_handler(handle_ring_buffer the_handler);
98 +extern int do_buffer_ring_handler(struct net_device *dev,
99 +                                 char *data, int len);
100 +#endif /* __KERNEL__  */
101 +
102 +/* *********************************** */
103 +
104 +#define PF_RING          27      /* Packet Ring */
105 +#define SOCK_RING        PF_RING
106 +
107 +/* ioctl() */
108 +#define SIORINGPOLL      0x8888
109 +
110 +/* *********************************** */
111 +
112 +#endif /* __RING_H */
113 diff --unified --recursive --new-file linux-2.6.12.5/net/Kconfig linux-2.6.12.5-1-686-smp-ring3/net/Kconfig
114 --- linux-2.6.12.5/net/Kconfig  2005-08-15 02:20:18.000000000 +0200
115 +++ linux-2.6.12.5-1-686-smp-ring3/net/Kconfig  2005-10-22 23:50:45.535481750 +0200
116 @@ -72,6 +72,7 @@
117  
118           Say Y unless you know what you are doing.
119  
120 +source "net/ring/Kconfig"
121  config INET
122         bool "TCP/IP networking"
123         ---help---
124 diff --unified --recursive --new-file linux-2.6.12.5/net/Makefile linux-2.6.12.5-1-686-smp-ring3/net/Makefile
125 --- linux-2.6.12.5/net/Makefile 2005-08-15 02:20:18.000000000 +0200
126 +++ linux-2.6.12.5-1-686-smp-ring3/net/Makefile 2005-10-22 23:50:45.491479000 +0200
127 @@ -41,6 +41,7 @@
128  obj-$(CONFIG_DECNET)           += decnet/
129  obj-$(CONFIG_ECONET)           += econet/
130  obj-$(CONFIG_VLAN_8021Q)       += 8021q/
131 +obj-$(CONFIG_RING)             += ring/
132  obj-$(CONFIG_IP_SCTP)          += sctp/
133  
134  ifeq ($(CONFIG_NET),y)
135 diff --unified --recursive --new-file linux-2.6.12.5/net/Makefile.ORG linux-2.6.12.5-1-686-smp-ring3/net/Makefile.ORG
136 --- linux-2.6.12.5/net/Makefile.ORG     1970-01-01 01:00:00.000000000 +0100
137 +++ linux-2.6.12.5-1-686-smp-ring3/net/Makefile.ORG     2005-10-22 23:50:45.483478500 +0200
138 @@ -0,0 +1,48 @@
139 +#
140 +# Makefile for the linux networking.
141 +#
142 +# 2 Sep 2000, Christoph Hellwig <hch@infradead.org>
143 +# Rewritten to use lists instead of if-statements.
144 +#
145 +
146 +obj-y  := nonet.o
147 +
148 +obj-$(CONFIG_NET)              := socket.o core/
149 +
150 +tmp-$(CONFIG_COMPAT)           := compat.o
151 +obj-$(CONFIG_NET)              += $(tmp-y)
152 +
153 +# LLC has to be linked before the files in net/802/
154 +obj-$(CONFIG_LLC)              += llc/
155 +obj-$(CONFIG_NET)              += ethernet/ 802/ sched/ netlink/
156 +obj-$(CONFIG_INET)             += ipv4/
157 +obj-$(CONFIG_XFRM)             += xfrm/
158 +obj-$(CONFIG_UNIX)             += unix/
159 +ifneq ($(CONFIG_IPV6),)
160 +obj-y                          += ipv6/
161 +endif
162 +obj-$(CONFIG_PACKET)           += packet/
163 +obj-$(CONFIG_NET_KEY)          += key/
164 +obj-$(CONFIG_NET_SCHED)                += sched/
165 +obj-$(CONFIG_BRIDGE)           += bridge/
166 +obj-$(CONFIG_IPX)              += ipx/
167 +obj-$(CONFIG_ATALK)            += appletalk/
168 +obj-$(CONFIG_WAN_ROUTER)       += wanrouter/
169 +obj-$(CONFIG_X25)              += x25/
170 +obj-$(CONFIG_LAPB)             += lapb/
171 +obj-$(CONFIG_NETROM)           += netrom/
172 +obj-$(CONFIG_ROSE)             += rose/
173 +obj-$(CONFIG_AX25)             += ax25/
174 +obj-$(CONFIG_IRDA)             += irda/
175 +obj-$(CONFIG_BT)               += bluetooth/
176 +obj-$(CONFIG_SUNRPC)           += sunrpc/
177 +obj-$(CONFIG_RXRPC)            += rxrpc/
178 +obj-$(CONFIG_ATM)              += atm/
179 +obj-$(CONFIG_DECNET)           += decnet/
180 +obj-$(CONFIG_ECONET)           += econet/
181 +obj-$(CONFIG_VLAN_8021Q)       += 8021q/
182 +obj-$(CONFIG_IP_SCTP)          += sctp/
183 +
184 +ifeq ($(CONFIG_NET),y)
185 +obj-$(CONFIG_SYSCTL)           += sysctl_net.o
186 +endif
187 diff --unified --recursive --new-file linux-2.6.12.5/net/core/dev.c linux-2.6.12.5-1-686-smp-ring3/net/core/dev.c
188 --- linux-2.6.12.5/net/core/dev.c       2005-08-15 02:20:18.000000000 +0200
189 +++ linux-2.6.12.5-1-686-smp-ring3/net/core/dev.c       2005-10-22 23:50:45.479478250 +0200
190 @@ -115,6 +115,56 @@
191  #endif /* CONFIG_NET_RADIO */
192  #include <asm/current.h>
193  
194 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
195 +
196 +/* #define RING_DEBUG */
197 +
198 +#include <linux/ring.h>
199 +#include <linux/version.h>
200 +
201 +static handle_ring_skb ring_handler = NULL;
202 +
203 +handle_ring_skb get_skb_ring_handler() { return(ring_handler); }
204 +
205 +void set_skb_ring_handler(handle_ring_skb the_handler) {
206 +  ring_handler = the_handler;
207 +}
208 +
209 +void do_skb_ring_handler(struct sk_buff *skb,
210 +                        u_char recv_packet, u_char real_skb) {
211 +  if(ring_handler)
212 +    ring_handler(skb, recv_packet, real_skb);
213 +}
214 +
215 +/* ******************* */
216 +
217 +static handle_ring_buffer buffer_ring_handler = NULL;
218 +
219 +handle_ring_buffer get_buffer_ring_handler() { return(buffer_ring_handler); }
220 +
221 +void set_buffer_ring_handler(handle_ring_buffer the_handler) {
222 +  buffer_ring_handler = the_handler;
223 +}
224 +
225 +int do_buffer_ring_handler(struct net_device *dev, char *data, int len) {
226 +  if(buffer_ring_handler) {
227 +    buffer_ring_handler(dev, data, len);
228 +    return(1);
229 +  } else 
230 +    return(0);
231 +}
232 +
233 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
234 +EXPORT_SYMBOL(get_skb_ring_handler);
235 +EXPORT_SYMBOL(set_skb_ring_handler);
236 +EXPORT_SYMBOL(do_skb_ring_handler);
237 +
238 +EXPORT_SYMBOL(get_buffer_ring_handler);
239 +EXPORT_SYMBOL(set_buffer_ring_handler);
240 +EXPORT_SYMBOL(do_buffer_ring_handler);
241 +#endif
242 +
243 +#endif
244  /* This define, if set, will randomly drop a packet when congestion
245   * is more than moderate.  It helps fairness in the multi-interface
246   * case when one of them is a hog, but it kills performance for the
247 @@ -1293,6 +1343,10 @@
248         skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
249  #endif
250         if (q->enqueue) {
251 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
252 +       if(ring_handler) ring_handler(skb, 0, 1);
253 +#endif /* CONFIG_RING */
254 +
255                 /* Grab device queue */
256                 spin_lock(&dev->queue_lock);
257  
258 @@ -1509,6 +1563,13 @@
259  
260         preempt_disable();
261         err = netif_rx(skb);
262 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
263 +       if(ring_handler && ring_handler(skb, 1, 1)) {
264 +         /* The packet has been copied into a ring */
265 +         return(NET_RX_SUCCESS);
266 +       }
267 +#endif /* CONFIG_RING */
268 +
269         if (local_softirq_pending())
270                 do_softirq();
271         preempt_enable();
272 @@ -1655,6 +1716,13 @@
273         int ret = NET_RX_DROP;
274         unsigned short type;
275  
276 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
277 +       if(ring_handler && ring_handler(skb, 1, 1)) {
278 +         /* The packet has been copied into a ring */
279 +         return(NET_RX_SUCCESS);
280 +       }
281 +#endif /* CONFIG_RING */
282 +
283         /* if we've gotten here through NAPI, check netpoll */
284         if (skb->dev->poll && netpoll_rx(skb))
285                 return NET_RX_DROP;
286 diff --unified --recursive --new-file linux-2.6.12.5/net/core/dev.c.ORG linux-2.6.12.5-1-686-smp-ring3/net/core/dev.c.ORG
287 --- linux-2.6.12.5/net/core/dev.c.ORG   1970-01-01 01:00:00.000000000 +0100
288 +++ linux-2.6.12.5-1-686-smp-ring3/net/core/dev.c.ORG   2005-10-22 23:50:45.203461000 +0200
289 @@ -0,0 +1,3385 @@
290 +/*
291 + *     NET3    Protocol independent device support routines.
292 + *
293 + *             This program is free software; you can redistribute it and/or
294 + *             modify it under the terms of the GNU General Public License
295 + *             as published by the Free Software Foundation; either version
296 + *             2 of the License, or (at your option) any later version.
297 + *
298 + *     Derived from the non IP parts of dev.c 1.0.19
299 + *             Authors:        Ross Biro
300 + *                             Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
301 + *                             Mark Evans, <evansmp@uhura.aston.ac.uk>
302 + *
303 + *     Additional Authors:
304 + *             Florian la Roche <rzsfl@rz.uni-sb.de>
305 + *             Alan Cox <gw4pts@gw4pts.ampr.org>
306 + *             David Hinds <dahinds@users.sourceforge.net>
307 + *             Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
308 + *             Adam Sulmicki <adam@cfar.umd.edu>
309 + *              Pekka Riikonen <priikone@poesidon.pspt.fi>
310 + *
311 + *     Changes:
312 + *              D.J. Barrow     :       Fixed bug where dev->refcnt gets set
313 + *                                     to 2 if register_netdev gets called
314 + *                                     before net_dev_init & also removed a
315 + *                                     few lines of code in the process.
316 + *             Alan Cox        :       device private ioctl copies fields back.
317 + *             Alan Cox        :       Transmit queue code does relevant
318 + *                                     stunts to keep the queue safe.
319 + *             Alan Cox        :       Fixed double lock.
320 + *             Alan Cox        :       Fixed promisc NULL pointer trap
321 + *             ????????        :       Support the full private ioctl range
322 + *             Alan Cox        :       Moved ioctl permission check into
323 + *                                     drivers
324 + *             Tim Kordas      :       SIOCADDMULTI/SIOCDELMULTI
325 + *             Alan Cox        :       100 backlog just doesn't cut it when
326 + *                                     you start doing multicast video 8)
327 + *             Alan Cox        :       Rewrote net_bh and list manager.
328 + *             Alan Cox        :       Fix ETH_P_ALL echoback lengths.
329 + *             Alan Cox        :       Took out transmit every packet pass
330 + *                                     Saved a few bytes in the ioctl handler
331 + *             Alan Cox        :       Network driver sets packet type before
332 + *                                     calling netif_rx. Saves a function
333 + *                                     call a packet.
334 + *             Alan Cox        :       Hashed net_bh()
335 + *             Richard Kooijman:       Timestamp fixes.
336 + *             Alan Cox        :       Wrong field in SIOCGIFDSTADDR
337 + *             Alan Cox        :       Device lock protection.
338 + *             Alan Cox        :       Fixed nasty side effect of device close
339 + *                                     changes.
340 + *             Rudi Cilibrasi  :       Pass the right thing to
341 + *                                     set_mac_address()
342 + *             Dave Miller     :       32bit quantity for the device lock to
343 + *                                     make it work out on a Sparc.
344 + *             Bjorn Ekwall    :       Added KERNELD hack.
345 + *             Alan Cox        :       Cleaned up the backlog initialise.
346 + *             Craig Metz      :       SIOCGIFCONF fix if space for under
347 + *                                     1 device.
348 + *         Thomas Bogendoerfer :       Return ENODEV for dev_open, if there
349 + *                                     is no device open function.
350 + *             Andi Kleen      :       Fix error reporting for SIOCGIFCONF
351 + *         Michael Chastain    :       Fix signed/unsigned for SIOCGIFCONF
352 + *             Cyrus Durgin    :       Cleaned for KMOD
353 + *             Adam Sulmicki   :       Bug Fix : Network Device Unload
354 + *                                     A network device unload needs to purge
355 + *                                     the backlog queue.
356 + *     Paul Rusty Russell      :       SIOCSIFNAME
357 + *              Pekka Riikonen  :      Netdev boot-time settings code
358 + *              Andrew Morton   :       Make unregister_netdevice wait
359 + *                                     indefinitely on dev->refcnt
360 + *             J Hadi Salim    :       - Backlog queue sampling
361 + *                                     - netif_rx() feedback
362 + */
363 +
364 +#include <asm/uaccess.h>
365 +#include <asm/system.h>
366 +#include <linux/bitops.h>
367 +#include <linux/config.h>
368 +#include <linux/cpu.h>
369 +#include <linux/types.h>
370 +#include <linux/kernel.h>
371 +#include <linux/sched.h>
372 +#include <linux/string.h>
373 +#include <linux/mm.h>
374 +#include <linux/socket.h>
375 +#include <linux/sockios.h>
376 +#include <linux/errno.h>
377 +#include <linux/interrupt.h>
378 +#include <linux/if_ether.h>
379 +#include <linux/netdevice.h>
380 +#include <linux/etherdevice.h>
381 +#include <linux/notifier.h>
382 +#include <linux/skbuff.h>
383 +#include <net/sock.h>
384 +#include <linux/rtnetlink.h>
385 +#include <linux/proc_fs.h>
386 +#include <linux/seq_file.h>
387 +#include <linux/stat.h>
388 +#include <linux/if_bridge.h>
389 +#include <linux/divert.h>
390 +#include <net/dst.h>
391 +#include <net/pkt_sched.h>
392 +#include <net/checksum.h>
393 +#include <linux/highmem.h>
394 +#include <linux/init.h>
395 +#include <linux/kmod.h>
396 +#include <linux/module.h>
397 +#include <linux/kallsyms.h>
398 +#include <linux/netpoll.h>
399 +#include <linux/rcupdate.h>
400 +#include <linux/delay.h>
401 +#ifdef CONFIG_NET_RADIO
402 +#include <linux/wireless.h>            /* Note : will define WIRELESS_EXT */
403 +#include <net/iw_handler.h>
404 +#endif /* CONFIG_NET_RADIO */
405 +#include <asm/current.h>
406 +
407 +/* This define, if set, will randomly drop a packet when congestion
408 + * is more than moderate.  It helps fairness in the multi-interface
409 + * case when one of them is a hog, but it kills performance for the
410 + * single interface case so it is off now by default.
411 + */
412 +#undef RAND_LIE
413 +
414 +/* Setting this will sample the queue lengths and thus congestion
415 + * via a timer instead of as each packet is received.
416 + */
417 +#undef OFFLINE_SAMPLE
418 +
419 +/*
420 + *     The list of packet types we will receive (as opposed to discard)
421 + *     and the routines to invoke.
422 + *
423 + *     Why 16. Because with 16 the only overlap we get on a hash of the
424 + *     low nibble of the protocol value is RARP/SNAP/X.25.
425 + *
426 + *      NOTE:  That is no longer true with the addition of VLAN tags.  Not
427 + *             sure which should go first, but I bet it won't make much
428 + *             difference if we are running VLANs.  The good news is that
429 + *             this protocol won't be in the list unless compiled in, so
430 + *             the average user (w/out VLANs) will not be adversly affected.
431 + *             --BLG
432 + *
433 + *             0800    IP
434 + *             8100    802.1Q VLAN
435 + *             0001    802.3
436 + *             0002    AX.25
437 + *             0004    802.2
438 + *             8035    RARP
439 + *             0005    SNAP
440 + *             0805    X.25
441 + *             0806    ARP
442 + *             8137    IPX
443 + *             0009    Localtalk
444 + *             86DD    IPv6
445 + */
446 +
447 +static DEFINE_SPINLOCK(ptype_lock);
448 +static struct list_head ptype_base[16];        /* 16 way hashed list */
449 +static struct list_head ptype_all;             /* Taps */
450 +
451 +#ifdef OFFLINE_SAMPLE
452 +static void sample_queue(unsigned long dummy);
453 +static struct timer_list samp_timer = TIMER_INITIALIZER(sample_queue, 0, 0);
454 +#endif
455 +
456 +/*
457 + * The @dev_base list is protected by @dev_base_lock and the rtln
458 + * semaphore.
459 + *
460 + * Pure readers hold dev_base_lock for reading.
461 + *
462 + * Writers must hold the rtnl semaphore while they loop through the
463 + * dev_base list, and hold dev_base_lock for writing when they do the
464 + * actual updates.  This allows pure readers to access the list even
465 + * while a writer is preparing to update it.
466 + *
467 + * To put it another way, dev_base_lock is held for writing only to
468 + * protect against pure readers; the rtnl semaphore provides the
469 + * protection against other writers.
470 + *
471 + * See, for example usages, register_netdevice() and
472 + * unregister_netdevice(), which must be called with the rtnl
473 + * semaphore held.
474 + */
475 +struct net_device *dev_base;
476 +static struct net_device **dev_tail = &dev_base;
477 +DEFINE_RWLOCK(dev_base_lock);
478 +
479 +EXPORT_SYMBOL(dev_base);
480 +EXPORT_SYMBOL(dev_base_lock);
481 +
482 +#define NETDEV_HASHBITS        8
483 +static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
484 +static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
485 +
486 +static inline struct hlist_head *dev_name_hash(const char *name)
487 +{
488 +       unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
489 +       return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
490 +}
491 +
492 +static inline struct hlist_head *dev_index_hash(int ifindex)
493 +{
494 +       return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
495 +}
496 +
497 +/*
498 + *     Our notifier list
499 + */
500 +
501 +static struct notifier_block *netdev_chain;
502 +
503 +/*
504 + *     Device drivers call our routines to queue packets here. We empty the
505 + *     queue in the local softnet handler.
506 + */
507 +DEFINE_PER_CPU(struct softnet_data, softnet_data) = { 0, };
508 +
509 +#ifdef CONFIG_SYSFS
510 +extern int netdev_sysfs_init(void);
511 +extern int netdev_register_sysfs(struct net_device *);
512 +extern void netdev_unregister_sysfs(struct net_device *);
513 +#else
514 +#define netdev_sysfs_init()            (0)
515 +#define netdev_register_sysfs(dev)     (0)
516 +#define        netdev_unregister_sysfs(dev)    do { } while(0)
517 +#endif
518 +
519 +
520 +/*******************************************************************************
521 +
522 +               Protocol management and registration routines
523 +
524 +*******************************************************************************/
525 +
526 +/*
527 + *     For efficiency
528 + */
529 +
530 +int netdev_nit;
531 +
532 +/*
533 + *     Add a protocol ID to the list. Now that the input handler is
534 + *     smarter we can dispense with all the messy stuff that used to be
535 + *     here.
536 + *
537 + *     BEWARE!!! Protocol handlers, mangling input packets,
538 + *     MUST BE last in hash buckets and checking protocol handlers
539 + *     MUST start from promiscuous ptype_all chain in net_bh.
540 + *     It is true now, do not change it.
541 + *     Explanation follows: if protocol handler, mangling packet, will
542 + *     be the first on list, it is not able to sense, that packet
543 + *     is cloned and should be copied-on-write, so that it will
544 + *     change it and subsequent readers will get broken packet.
545 + *                                                     --ANK (980803)
546 + */
547 +
548 +/**
549 + *     dev_add_pack - add packet handler
550 + *     @pt: packet type declaration
551 + *
552 + *     Add a protocol handler to the networking stack. The passed &packet_type
553 + *     is linked into kernel lists and may not be freed until it has been
554 + *     removed from the kernel lists.
555 + *
556 + *     This call does not sleep therefore it can not 
557 + *     guarantee all CPU's that are in middle of receiving packets
558 + *     will see the new packet type (until the next received packet).
559 + */
560 +
561 +void dev_add_pack(struct packet_type *pt)
562 +{
563 +       int hash;
564 +
565 +       spin_lock_bh(&ptype_lock);
566 +       if (pt->type == htons(ETH_P_ALL)) {
567 +               netdev_nit++;
568 +               list_add_rcu(&pt->list, &ptype_all);
569 +       } else {
570 +               hash = ntohs(pt->type) & 15;
571 +               list_add_rcu(&pt->list, &ptype_base[hash]);
572 +       }
573 +       spin_unlock_bh(&ptype_lock);
574 +}
575 +
576 +extern void linkwatch_run_queue(void);
577 +
578 +
579 +
580 +/**
581 + *     __dev_remove_pack        - remove packet handler
582 + *     @pt: packet type declaration
583 + *
584 + *     Remove a protocol handler that was previously added to the kernel
585 + *     protocol handlers by dev_add_pack(). The passed &packet_type is removed
586 + *     from the kernel lists and can be freed or reused once this function
587 + *     returns. 
588 + *
589 + *      The packet type might still be in use by receivers
590 + *     and must not be freed until after all the CPU's have gone
591 + *     through a quiescent state.
592 + */
593 +void __dev_remove_pack(struct packet_type *pt)
594 +{
595 +       struct list_head *head;
596 +       struct packet_type *pt1;
597 +
598 +       spin_lock_bh(&ptype_lock);
599 +
600 +       if (pt->type == htons(ETH_P_ALL)) {
601 +               netdev_nit--;
602 +               head = &ptype_all;
603 +       } else
604 +               head = &ptype_base[ntohs(pt->type) & 15];
605 +
606 +       list_for_each_entry(pt1, head, list) {
607 +               if (pt == pt1) {
608 +                       list_del_rcu(&pt->list);
609 +                       goto out;
610 +               }
611 +       }
612 +
613 +       printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
614 +out:
615 +       spin_unlock_bh(&ptype_lock);
616 +}
617 +/**
618 + *     dev_remove_pack  - remove packet handler
619 + *     @pt: packet type declaration
620 + *
621 + *     Remove a protocol handler that was previously added to the kernel
622 + *     protocol handlers by dev_add_pack(). The passed &packet_type is removed
623 + *     from the kernel lists and can be freed or reused once this function
624 + *     returns.
625 + *
626 + *     This call sleeps to guarantee that no CPU is looking at the packet
627 + *     type after return.
628 + */
629 +void dev_remove_pack(struct packet_type *pt)
630 +{
631 +       __dev_remove_pack(pt);
632 +       
633 +       synchronize_net();
634 +}
635 +
636 +/******************************************************************************
637 +
638 +                     Device Boot-time Settings Routines
639 +
640 +*******************************************************************************/
641 +
642 +/* Boot time configuration table */
643 +static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
644 +
645 +/**
646 + *     netdev_boot_setup_add   - add new setup entry
647 + *     @name: name of the device
648 + *     @map: configured settings for the device
649 + *
650 + *     Adds new setup entry to the dev_boot_setup list.  The function
651 + *     returns 0 on error and 1 on success.  This is a generic routine to
652 + *     all netdevices.
653 + */
654 +static int netdev_boot_setup_add(char *name, struct ifmap *map)
655 +{
656 +       struct netdev_boot_setup *s;
657 +       int i;
658 +
659 +       s = dev_boot_setup;
660 +       for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
661 +               if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
662 +                       memset(s[i].name, 0, sizeof(s[i].name));
663 +                       strcpy(s[i].name, name);
664 +                       memcpy(&s[i].map, map, sizeof(s[i].map));
665 +                       break;
666 +               }
667 +       }
668 +
669 +       return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
670 +}
671 +
672 +/**
673 + *     netdev_boot_setup_check - check boot time settings
674 + *     @dev: the netdevice
675 + *
676 + *     Check boot time settings for the device.
677 + *     The found settings are set for the device to be used
678 + *     later in the device probing.
679 + *     Returns 0 if no settings found, 1 if they are.
680 + */
681 +int netdev_boot_setup_check(struct net_device *dev)
682 +{
683 +       struct netdev_boot_setup *s = dev_boot_setup;
684 +       int i;
685 +
686 +       for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
687 +               if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
688 +                   !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
689 +                       dev->irq        = s[i].map.irq;
690 +                       dev->base_addr  = s[i].map.base_addr;
691 +                       dev->mem_start  = s[i].map.mem_start;
692 +                       dev->mem_end    = s[i].map.mem_end;
693 +                       return 1;
694 +               }
695 +       }
696 +       return 0;
697 +}
698 +
699 +
700 +/**
701 + *     netdev_boot_base        - get address from boot time settings
702 + *     @prefix: prefix for network device
703 + *     @unit: id for network device
704 + *
705 + *     Check boot time settings for the base address of device.
706 + *     The found settings are set for the device to be used
707 + *     later in the device probing.
708 + *     Returns 0 if no settings found.
709 + */
710 +unsigned long netdev_boot_base(const char *prefix, int unit)
711 +{
712 +       const struct netdev_boot_setup *s = dev_boot_setup;
713 +       char name[IFNAMSIZ];
714 +       int i;
715 +
716 +       sprintf(name, "%s%d", prefix, unit);
717 +
718 +       /*
719 +        * If device already registered then return base of 1
720 +        * to indicate not to probe for this interface
721 +        */
722 +       if (__dev_get_by_name(name))
723 +               return 1;
724 +
725 +       for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
726 +               if (!strcmp(name, s[i].name))
727 +                       return s[i].map.base_addr;
728 +       return 0;
729 +}
730 +
731 +/*
732 + * Saves at boot time configured settings for any netdevice.
733 + */
734 +int __init netdev_boot_setup(char *str)
735 +{
736 +       int ints[5];
737 +       struct ifmap map;
738 +
739 +       str = get_options(str, ARRAY_SIZE(ints), ints);
740 +       if (!str || !*str)
741 +               return 0;
742 +
743 +       /* Save settings */
744 +       memset(&map, 0, sizeof(map));
745 +       if (ints[0] > 0)
746 +               map.irq = ints[1];
747 +       if (ints[0] > 1)
748 +               map.base_addr = ints[2];
749 +       if (ints[0] > 2)
750 +               map.mem_start = ints[3];
751 +       if (ints[0] > 3)
752 +               map.mem_end = ints[4];
753 +
754 +       /* Add new entry to the list */
755 +       return netdev_boot_setup_add(str, &map);
756 +}
757 +
758 +__setup("netdev=", netdev_boot_setup);
759 +
760 +/*******************************************************************************
761 +
762 +                           Device Interface Subroutines
763 +
764 +*******************************************************************************/
765 +
766 +/**
767 + *     __dev_get_by_name       - find a device by its name
768 + *     @name: name to find
769 + *
770 + *     Find an interface by name. Must be called under RTNL semaphore
771 + *     or @dev_base_lock. If the name is found a pointer to the device
772 + *     is returned. If the name is not found then %NULL is returned. The
773 + *     reference counters are not incremented so the caller must be
774 + *     careful with locks.
775 + */
776 +
777 +struct net_device *__dev_get_by_name(const char *name)
778 +{
779 +       struct hlist_node *p;
780 +
781 +       hlist_for_each(p, dev_name_hash(name)) {
782 +               struct net_device *dev
783 +                       = hlist_entry(p, struct net_device, name_hlist);
784 +               if (!strncmp(dev->name, name, IFNAMSIZ))
785 +                       return dev;
786 +       }
787 +       return NULL;
788 +}
789 +
790 +/**
791 + *     dev_get_by_name         - find a device by its name
792 + *     @name: name to find
793 + *
794 + *     Find an interface by name. This can be called from any
795 + *     context and does its own locking. The returned handle has
796 + *     the usage count incremented and the caller must use dev_put() to
797 + *     release it when it is no longer needed. %NULL is returned if no
798 + *     matching device is found.
799 + */
800 +
801 +struct net_device *dev_get_by_name(const char *name)
802 +{
803 +       struct net_device *dev;
804 +
805 +       read_lock(&dev_base_lock);
806 +       dev = __dev_get_by_name(name);
807 +       if (dev)
808 +               dev_hold(dev);
809 +       read_unlock(&dev_base_lock);
810 +       return dev;
811 +}
812 +
813 +/**
814 + *     __dev_get_by_index - find a device by its ifindex
815 + *     @ifindex: index of device
816 + *
817 + *     Search for an interface by index. Returns %NULL if the device
818 + *     is not found or a pointer to the device. The device has not
819 + *     had its reference counter increased so the caller must be careful
820 + *     about locking. The caller must hold either the RTNL semaphore
821 + *     or @dev_base_lock.
822 + */
823 +
824 +struct net_device *__dev_get_by_index(int ifindex)
825 +{
826 +       struct hlist_node *p;
827 +
828 +       hlist_for_each(p, dev_index_hash(ifindex)) {
829 +               struct net_device *dev
830 +                       = hlist_entry(p, struct net_device, index_hlist);
831 +               if (dev->ifindex == ifindex)
832 +                       return dev;
833 +       }
834 +       return NULL;
835 +}
836 +
837 +
838 +/**
839 + *     dev_get_by_index - find a device by its ifindex
840 + *     @ifindex: index of device
841 + *
842 + *     Search for an interface by index. Returns NULL if the device
843 + *     is not found or a pointer to the device. The device returned has
844 + *     had a reference added and the pointer is safe until the user calls
845 + *     dev_put to indicate they have finished with it.
846 + */
847 +
848 +struct net_device *dev_get_by_index(int ifindex)
849 +{
850 +       struct net_device *dev;
851 +
852 +       read_lock(&dev_base_lock);
853 +       dev = __dev_get_by_index(ifindex);
854 +       if (dev)
855 +               dev_hold(dev);
856 +       read_unlock(&dev_base_lock);
857 +       return dev;
858 +}
859 +
860 +/**
861 + *     dev_getbyhwaddr - find a device by its hardware address
862 + *     @type: media type of device
863 + *     @ha: hardware address
864 + *
865 + *     Search for an interface by MAC address. Returns NULL if the device
866 + *     is not found or a pointer to the device. The caller must hold the
867 + *     rtnl semaphore. The returned device has not had its ref count increased
868 + *     and the caller must therefore be careful about locking
869 + *
870 + *     BUGS:
871 + *     If the API was consistent this would be __dev_get_by_hwaddr
872 + */
873 +
874 +struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
875 +{
876 +       struct net_device *dev;
877 +
878 +       ASSERT_RTNL();
879 +
880 +       for (dev = dev_base; dev; dev = dev->next)
881 +               if (dev->type == type &&
882 +                   !memcmp(dev->dev_addr, ha, dev->addr_len))
883 +                       break;
884 +       return dev;
885 +}
886 +
887 +struct net_device *dev_getfirstbyhwtype(unsigned short type)
888 +{
889 +       struct net_device *dev;
890 +
891 +       rtnl_lock();
892 +       for (dev = dev_base; dev; dev = dev->next) {
893 +               if (dev->type == type) {
894 +                       dev_hold(dev);
895 +                       break;
896 +               }
897 +       }
898 +       rtnl_unlock();
899 +       return dev;
900 +}
901 +
902 +EXPORT_SYMBOL(dev_getfirstbyhwtype);
903 +
904 +/**
905 + *     dev_get_by_flags - find any device with given flags
906 + *     @if_flags: IFF_* values
907 + *     @mask: bitmask of bits in if_flags to check
908 + *
909 + *     Search for any interface with the given flags. Returns NULL if a device
910 + *     is not found or a pointer to the device. The device returned has 
911 + *     had a reference added and the pointer is safe until the user calls
912 + *     dev_put to indicate they have finished with it.
913 + */
914 +
915 +struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
916 +{
917 +       struct net_device *dev;
918 +
919 +       read_lock(&dev_base_lock);
920 +       for (dev = dev_base; dev != NULL; dev = dev->next) {
921 +               if (((dev->flags ^ if_flags) & mask) == 0) {
922 +                       dev_hold(dev);
923 +                       break;
924 +               }
925 +       }
926 +       read_unlock(&dev_base_lock);
927 +       return dev;
928 +}
929 +
930 +/**
931 + *     dev_valid_name - check if name is okay for network device
932 + *     @name: name string
933 + *
934 + *     Network device names need to be valid file names to
935 + *     to allow sysfs to work
936 + */
937 +static int dev_valid_name(const char *name)
938 +{
939 +       return !(*name == '\0' 
940 +                || !strcmp(name, ".")
941 +                || !strcmp(name, "..")
942 +                || strchr(name, '/'));
943 +}
944 +
945 +/**
946 + *     dev_alloc_name - allocate a name for a device
947 + *     @dev: device
948 + *     @name: name format string
949 + *
950 + *     Passed a format string - eg "lt%d" it will try and find a suitable
951 + *     id. Not efficient for many devices, not called a lot. The caller
952 + *     must hold the dev_base or rtnl lock while allocating the name and
953 + *     adding the device in order to avoid duplicates. Returns the number
954 + *     of the unit assigned or a negative errno code.
955 + */
956 +
957 +int dev_alloc_name(struct net_device *dev, const char *name)
958 +{
959 +       int i = 0;
960 +       char buf[IFNAMSIZ];
961 +       const char *p;
962 +       const int max_netdevices = 8*PAGE_SIZE;
963 +       long *inuse;
964 +       struct net_device *d;
965 +
966 +       p = strnchr(name, IFNAMSIZ-1, '%');
967 +       if (p) {
968 +               /*
969 +                * Verify the string as this thing may have come from
970 +                * the user.  There must be either one "%d" and no other "%"
971 +                * characters.
972 +                */
973 +               if (p[1] != 'd' || strchr(p + 2, '%'))
974 +                       return -EINVAL;
975 +
976 +               /* Use one page as a bit array of possible slots */
977 +               inuse = (long *) get_zeroed_page(GFP_ATOMIC);
978 +               if (!inuse)
979 +                       return -ENOMEM;
980 +
981 +               for (d = dev_base; d; d = d->next) {
982 +                       if (!sscanf(d->name, name, &i))
983 +                               continue;
984 +                       if (i < 0 || i >= max_netdevices)
985 +                               continue;
986 +
987 +                       /*  avoid cases where sscanf is not exact inverse of printf */
988 +                       snprintf(buf, sizeof(buf), name, i);
989 +                       if (!strncmp(buf, d->name, IFNAMSIZ))
990 +                               set_bit(i, inuse);
991 +               }
992 +
993 +               i = find_first_zero_bit(inuse, max_netdevices);
994 +               free_page((unsigned long) inuse);
995 +       }
996 +
997 +       snprintf(buf, sizeof(buf), name, i);
998 +       if (!__dev_get_by_name(buf)) {
999 +               strlcpy(dev->name, buf, IFNAMSIZ);
1000 +               return i;
1001 +       }
1002 +
1003 +       /* It is possible to run out of possible slots
1004 +        * when the name is long and there isn't enough space left
1005 +        * for the digits, or if all bits are used.
1006 +        */
1007 +       return -ENFILE;
1008 +}
1009 +
1010 +
1011 +/**
1012 + *     dev_change_name - change name of a device
1013 + *     @dev: device
1014 + *     @newname: name (or format string) must be at least IFNAMSIZ
1015 + *
1016 + *     Change name of a device, can pass format strings "eth%d".
1017 + *     for wildcarding.
1018 + */
1019 +int dev_change_name(struct net_device *dev, char *newname)
1020 +{
1021 +       int err = 0;
1022 +
1023 +       ASSERT_RTNL();
1024 +
1025 +       if (dev->flags & IFF_UP)
1026 +               return -EBUSY;
1027 +
1028 +       if (!dev_valid_name(newname))
1029 +               return -EINVAL;
1030 +
1031 +       if (strchr(newname, '%')) {
1032 +               err = dev_alloc_name(dev, newname);
1033 +               if (err < 0)
1034 +                       return err;
1035 +               strcpy(newname, dev->name);
1036 +       }
1037 +       else if (__dev_get_by_name(newname))
1038 +               return -EEXIST;
1039 +       else
1040 +               strlcpy(dev->name, newname, IFNAMSIZ);
1041 +
1042 +       err = class_device_rename(&dev->class_dev, dev->name);
1043 +       if (!err) {
1044 +               hlist_del(&dev->name_hlist);
1045 +               hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
1046 +               notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
1047 +       }
1048 +
1049 +       return err;
1050 +}
1051 +
1052 +/**
1053 + *     netdev_features_change - device changes fatures
1054 + *     @dev: device to cause notification
1055 + *
1056 + *     Called to indicate a device has changed features.
1057 + */
1058 +void netdev_features_change(struct net_device *dev)
1059 +{
1060 +       notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
1061 +}
1062 +EXPORT_SYMBOL(netdev_features_change);
1063 +
1064 +/**
1065 + *     netdev_state_change - device changes state
1066 + *     @dev: device to cause notification
1067 + *
1068 + *     Called to indicate a device has changed state. This function calls
1069 + *     the notifier chains for netdev_chain and sends a NEWLINK message
1070 + *     to the routing socket.
1071 + */
1072 +void netdev_state_change(struct net_device *dev)
1073 +{
1074 +       if (dev->flags & IFF_UP) {
1075 +               notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
1076 +               rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1077 +       }
1078 +}
1079 +
1080 +/**
1081 + *     dev_load        - load a network module
1082 + *     @name: name of interface
1083 + *
1084 + *     If a network interface is not present and the process has suitable
1085 + *     privileges this function loads the module. If module loading is not
1086 + *     available in this kernel then it becomes a nop.
1087 + */
1088 +
1089 +void dev_load(const char *name)
1090 +{
1091 +       struct net_device *dev;  
1092 +
1093 +       read_lock(&dev_base_lock);
1094 +       dev = __dev_get_by_name(name);
1095 +       read_unlock(&dev_base_lock);
1096 +
1097 +       if (!dev && capable(CAP_SYS_MODULE))
1098 +               request_module("%s", name);
1099 +}
1100 +
1101 +static int default_rebuild_header(struct sk_buff *skb)
1102 +{
1103 +       printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
1104 +              skb->dev ? skb->dev->name : "NULL!!!");
1105 +       kfree_skb(skb);
1106 +       return 1;
1107 +}
1108 +
1109 +
1110 +/**
1111 + *     dev_open        - prepare an interface for use.
1112 + *     @dev:   device to open
1113 + *
1114 + *     Takes a device from down to up state. The device's private open
1115 + *     function is invoked and then the multicast lists are loaded. Finally
1116 + *     the device is moved into the up state and a %NETDEV_UP message is
1117 + *     sent to the netdev notifier chain.
1118 + *
1119 + *     Calling this function on an active interface is a nop. On a failure
1120 + *     a negative errno code is returned.
1121 + */
1122 +int dev_open(struct net_device *dev)
1123 +{
1124 +       int ret = 0;
1125 +
1126 +       /*
1127 +        *      Is it already up?
1128 +        */
1129 +
1130 +       if (dev->flags & IFF_UP)
1131 +               return 0;
1132 +
1133 +       /*
1134 +        *      Is it even present?
1135 +        */
1136 +       if (!netif_device_present(dev))
1137 +               return -ENODEV;
1138 +
1139 +       /*
1140 +        *      Call device private open method
1141 +        */
1142 +       set_bit(__LINK_STATE_START, &dev->state);
1143 +       if (dev->open) {
1144 +               ret = dev->open(dev);
1145 +               if (ret)
1146 +                       clear_bit(__LINK_STATE_START, &dev->state);
1147 +       }
1148 +
1149 +       /*
1150 +        *      If it went open OK then:
1151 +        */
1152 +
1153 +       if (!ret) {
1154 +               /*
1155 +                *      Set the flags.
1156 +                */
1157 +               dev->flags |= IFF_UP;
1158 +
1159 +               /*
1160 +                *      Initialize multicasting status
1161 +                */
1162 +               dev_mc_upload(dev);
1163 +
1164 +               /*
1165 +                *      Wakeup transmit queue engine
1166 +                */
1167 +               dev_activate(dev);
1168 +
1169 +               /*
1170 +                *      ... and announce new interface.
1171 +                */
1172 +               notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
1173 +       }
1174 +       return ret;
1175 +}
1176 +
1177 +/**
1178 + *     dev_close - shutdown an interface.
1179 + *     @dev: device to shutdown
1180 + *
1181 + *     This function moves an active device into down state. A
1182 + *     %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1183 + *     is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1184 + *     chain.
1185 + */
1186 +int dev_close(struct net_device *dev)
1187 +{
1188 +       if (!(dev->flags & IFF_UP))
1189 +               return 0;
1190 +
1191 +       /*
1192 +        *      Tell people we are going down, so that they can
1193 +        *      prepare to death, when device is still operating.
1194 +        */
1195 +       notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
1196 +
1197 +       dev_deactivate(dev);
1198 +
1199 +       clear_bit(__LINK_STATE_START, &dev->state);
1200 +
1201 +       /* Synchronize to scheduled poll. We cannot touch poll list,
1202 +        * it can be even on different cpu. So just clear netif_running(),
1203 +        * and wait when poll really will happen. Actually, the best place
1204 +        * for this is inside dev->stop() after device stopped its irq
1205 +        * engine, but this requires more changes in devices. */
1206 +
1207 +       smp_mb__after_clear_bit(); /* Commit netif_running(). */
1208 +       while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
1209 +               /* No hurry. */
1210 +               current->state = TASK_INTERRUPTIBLE;
1211 +               schedule_timeout(1);
1212 +       }
1213 +
1214 +       /*
1215 +        *      Call the device specific close. This cannot fail.
1216 +        *      Only if device is UP
1217 +        *
1218 +        *      We allow it to be called even after a DETACH hot-plug
1219 +        *      event.
1220 +        */
1221 +       if (dev->stop)
1222 +               dev->stop(dev);
1223 +
1224 +       /*
1225 +        *      Device is now down.
1226 +        */
1227 +
1228 +       dev->flags &= ~IFF_UP;
1229 +
1230 +       /*
1231 +        * Tell people we are down
1232 +        */
1233 +       notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
1234 +
1235 +       return 0;
1236 +}
1237 +
1238 +
1239 +/*
1240 + *     Device change register/unregister. These are not inline or static
1241 + *     as we export them to the world.
1242 + */
1243 +
1244 +/**
1245 + *     register_netdevice_notifier - register a network notifier block
1246 + *     @nb: notifier
1247 + *
1248 + *     Register a notifier to be called when network device events occur.
1249 + *     The notifier passed is linked into the kernel structures and must
1250 + *     not be reused until it has been unregistered. A negative errno code
1251 + *     is returned on a failure.
1252 + *
1253 + *     When registered all registration and up events are replayed
1254 + *     to the new notifier to allow device to have a race free 
1255 + *     view of the network device list.
1256 + */
1257 +
1258 +int register_netdevice_notifier(struct notifier_block *nb)
1259 +{
1260 +       struct net_device *dev;
1261 +       int err;
1262 +
1263 +       rtnl_lock();
1264 +       err = notifier_chain_register(&netdev_chain, nb);
1265 +       if (!err) {
1266 +               for (dev = dev_base; dev; dev = dev->next) {
1267 +                       nb->notifier_call(nb, NETDEV_REGISTER, dev);
1268 +
1269 +                       if (dev->flags & IFF_UP) 
1270 +                               nb->notifier_call(nb, NETDEV_UP, dev);
1271 +               }
1272 +       }
1273 +       rtnl_unlock();
1274 +       return err;
1275 +}
1276 +
1277 +/**
1278 + *     unregister_netdevice_notifier - unregister a network notifier block
1279 + *     @nb: notifier
1280 + *
1281 + *     Unregister a notifier previously registered by
1282 + *     register_netdevice_notifier(). The notifier is unlinked into the
1283 + *     kernel structures and may then be reused. A negative errno code
1284 + *     is returned on a failure.
1285 + */
1286 +
1287 +int unregister_netdevice_notifier(struct notifier_block *nb)
1288 +{
1289 +       return notifier_chain_unregister(&netdev_chain, nb);
1290 +}
1291 +
1292 +/**
1293 + *     call_netdevice_notifiers - call all network notifier blocks
1294 + *      @val: value passed unmodified to notifier function
1295 + *      @v:   pointer passed unmodified to notifier function
1296 + *
1297 + *     Call all network notifier blocks.  Parameters and return value
1298 + *     are as for notifier_call_chain().
1299 + */
1300 +
1301 +int call_netdevice_notifiers(unsigned long val, void *v)
1302 +{
1303 +       return notifier_call_chain(&netdev_chain, val, v);
1304 +}
1305 +
1306 +/* When > 0 there are consumers of rx skb time stamps */
1307 +static atomic_t netstamp_needed = ATOMIC_INIT(0);
1308 +
1309 +void net_enable_timestamp(void)
1310 +{
1311 +       atomic_inc(&netstamp_needed);
1312 +}
1313 +
1314 +void net_disable_timestamp(void)
1315 +{
1316 +       atomic_dec(&netstamp_needed);
1317 +}
1318 +
1319 +static inline void net_timestamp(struct timeval *stamp)
1320 +{
1321 +       if (atomic_read(&netstamp_needed))
1322 +               do_gettimeofday(stamp);
1323 +       else {
1324 +               stamp->tv_sec = 0;
1325 +               stamp->tv_usec = 0;
1326 +       }
1327 +}
1328 +
1329 +/*
1330 + *     Support routine. Sends outgoing frames to any network
1331 + *     taps currently in use.
1332 + */
1333 +
1334 +void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1335 +{
1336 +       struct packet_type *ptype;
1337 +       net_timestamp(&skb->stamp);
1338 +
1339 +       rcu_read_lock();
1340 +       list_for_each_entry_rcu(ptype, &ptype_all, list) {
1341 +               /* Never send packets back to the socket
1342 +                * they originated from - MvS (miquels@drinkel.ow.org)
1343 +                */
1344 +               if ((ptype->dev == dev || !ptype->dev) &&
1345 +                   (ptype->af_packet_priv == NULL ||
1346 +                    (struct sock *)ptype->af_packet_priv != skb->sk)) {
1347 +                       struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1348 +                       if (!skb2)
1349 +                               break;
1350 +
1351 +                       /* skb->nh should be correctly
1352 +                          set by sender, so that the second statement is
1353 +                          just protection against buggy protocols.
1354 +                        */
1355 +                       skb2->mac.raw = skb2->data;
1356 +
1357 +                       if (skb2->nh.raw < skb2->data ||
1358 +                           skb2->nh.raw > skb2->tail) {
1359 +                               if (net_ratelimit())
1360 +                                       printk(KERN_CRIT "protocol %04x is "
1361 +                                              "buggy, dev %s\n",
1362 +                                              skb2->protocol, dev->name);
1363 +                               skb2->nh.raw = skb2->data;
1364 +                       }
1365 +
1366 +                       skb2->h.raw = skb2->nh.raw;
1367 +                       skb2->pkt_type = PACKET_OUTGOING;
1368 +                       ptype->func(skb2, skb->dev, ptype);
1369 +               }
1370 +       }
1371 +       rcu_read_unlock();
1372 +}
1373 +
1374 +/*
1375 + * Invalidate hardware checksum when packet is to be mangled, and
1376 + * complete checksum manually on outgoing path.
1377 + */
1378 +int skb_checksum_help(struct sk_buff *skb, int inward)
1379 +{
1380 +       unsigned int csum;
1381 +       int ret = 0, offset = skb->h.raw - skb->data;
1382 +
1383 +       if (inward) {
1384 +               skb->ip_summed = CHECKSUM_NONE;
1385 +               goto out;
1386 +       }
1387 +
1388 +       if (skb_cloned(skb)) {
1389 +               ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1390 +               if (ret)
1391 +                       goto out;
1392 +       }
1393 +
1394 +       if (offset > (int)skb->len)
1395 +               BUG();
1396 +       csum = skb_checksum(skb, offset, skb->len-offset, 0);
1397 +
1398 +       offset = skb->tail - skb->h.raw;
1399 +       if (offset <= 0)
1400 +               BUG();
1401 +       if (skb->csum + 2 > offset)
1402 +               BUG();
1403 +
1404 +       *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
1405 +       skb->ip_summed = CHECKSUM_NONE;
1406 +out:   
1407 +       return ret;
1408 +}
1409 +
1410 +#ifdef CONFIG_HIGHMEM
1411 +/* Actually, we should eliminate this check as soon as we know, that:
1412 + * 1. IOMMU is present and allows to map all the memory.
1413 + * 2. No high memory really exists on this machine.
1414 + */
1415 +
1416 +static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1417 +{
1418 +       int i;
1419 +
1420 +       if (dev->features & NETIF_F_HIGHDMA)
1421 +               return 0;
1422 +
1423 +       for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1424 +               if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1425 +                       return 1;
1426 +
1427 +       return 0;
1428 +}
1429 +#else
1430 +#define illegal_highdma(dev, skb)      (0)
1431 +#endif
1432 +
1433 +extern void skb_release_data(struct sk_buff *);
1434 +
1435 +/* Keep head the same: replace data */
1436 +int __skb_linearize(struct sk_buff *skb, int gfp_mask)
1437 +{
1438 +       unsigned int size;
1439 +       u8 *data;
1440 +       long offset;
1441 +       struct skb_shared_info *ninfo;
1442 +       int headerlen = skb->data - skb->head;
1443 +       int expand = (skb->tail + skb->data_len) - skb->end;
1444 +
1445 +       if (skb_shared(skb))
1446 +               BUG();
1447 +
1448 +       if (expand <= 0)
1449 +               expand = 0;
1450 +
1451 +       size = skb->end - skb->head + expand;
1452 +       size = SKB_DATA_ALIGN(size);
1453 +       data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
1454 +       if (!data)
1455 +               return -ENOMEM;
1456 +
1457 +       /* Copy entire thing */
1458 +       if (skb_copy_bits(skb, -headerlen, data, headerlen + skb->len))
1459 +               BUG();
1460 +
1461 +       /* Set up shinfo */
1462 +       ninfo = (struct skb_shared_info*)(data + size);
1463 +       atomic_set(&ninfo->dataref, 1);
1464 +       ninfo->tso_size = skb_shinfo(skb)->tso_size;
1465 +       ninfo->tso_segs = skb_shinfo(skb)->tso_segs;
1466 +       ninfo->nr_frags = 0;
1467 +       ninfo->frag_list = NULL;
1468 +
1469 +       /* Offset between the two in bytes */
1470 +       offset = data - skb->head;
1471 +
1472 +       /* Free old data. */
1473 +       skb_release_data(skb);
1474 +
1475 +       skb->head = data;
1476 +       skb->end  = data + size;
1477 +
1478 +       /* Set up new pointers */
1479 +       skb->h.raw   += offset;
1480 +       skb->nh.raw  += offset;
1481 +       skb->mac.raw += offset;
1482 +       skb->tail    += offset;
1483 +       skb->data    += offset;
1484 +
1485 +       /* We are no longer a clone, even if we were. */
1486 +       skb->cloned    = 0;
1487 +
1488 +       skb->tail     += skb->data_len;
1489 +       skb->data_len  = 0;
1490 +       return 0;
1491 +}
1492 +
1493 +#define HARD_TX_LOCK(dev, cpu) {                       \
1494 +       if ((dev->features & NETIF_F_LLTX) == 0) {      \
1495 +               spin_lock(&dev->xmit_lock);             \
1496 +               dev->xmit_lock_owner = cpu;             \
1497 +       }                                               \
1498 +}
1499 +
1500 +#define HARD_TX_UNLOCK(dev) {                          \
1501 +       if ((dev->features & NETIF_F_LLTX) == 0) {      \
1502 +               dev->xmit_lock_owner = -1;              \
1503 +               spin_unlock(&dev->xmit_lock);           \
1504 +       }                                               \
1505 +}
1506 +
1507 +/**
1508 + *     dev_queue_xmit - transmit a buffer
1509 + *     @skb: buffer to transmit
1510 + *
1511 + *     Queue a buffer for transmission to a network device. The caller must
1512 + *     have set the device and priority and built the buffer before calling
1513 + *     this function. The function can be called from an interrupt.
1514 + *
1515 + *     A negative errno code is returned on a failure. A success does not
1516 + *     guarantee the frame will be transmitted as it may be dropped due
1517 + *     to congestion or traffic shaping.
1518 + *
1519 + * -----------------------------------------------------------------------------------
1520 + *      I notice this method can also return errors from the queue disciplines,
1521 + *      including NET_XMIT_DROP, which is a positive value.  So, errors can also
1522 + *      be positive.
1523 + *
1524 + *      Regardless of the return value, the skb is consumed, so it is currently
1525 + *      difficult to retry a send to this method.  (You can bump the ref count
1526 + *      before sending to hold a reference for retry if you are careful.)
1527 + *
1528 + *      When calling this method, interrupts MUST be enabled.  This is because
1529 + *      the BH enable code must have IRQs enabled so that it will not deadlock.
1530 + *          --BLG
1531 + */
1532 +
1533 +int dev_queue_xmit(struct sk_buff *skb)
1534 +{
1535 +       struct net_device *dev = skb->dev;
1536 +       struct Qdisc *q;
1537 +       int rc = -ENOMEM;
1538 +
1539 +       if (skb_shinfo(skb)->frag_list &&
1540 +           !(dev->features & NETIF_F_FRAGLIST) &&
1541 +           __skb_linearize(skb, GFP_ATOMIC))
1542 +               goto out_kfree_skb;
1543 +
1544 +       /* Fragmented skb is linearized if device does not support SG,
1545 +        * or if at least one of fragments is in highmem and device
1546 +        * does not support DMA from it.
1547 +        */
1548 +       if (skb_shinfo(skb)->nr_frags &&
1549 +           (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1550 +           __skb_linearize(skb, GFP_ATOMIC))
1551 +               goto out_kfree_skb;
1552 +
1553 +       /* If packet is not checksummed and device does not support
1554 +        * checksumming for this protocol, complete checksumming here.
1555 +        */
1556 +       if (skb->ip_summed == CHECKSUM_HW &&
1557 +           (!(dev->features & (NETIF_F_HW_CSUM | NETIF_F_NO_CSUM)) &&
1558 +            (!(dev->features & NETIF_F_IP_CSUM) ||
1559 +             skb->protocol != htons(ETH_P_IP))))
1560 +               if (skb_checksum_help(skb, 0))
1561 +                       goto out_kfree_skb;
1562 +
1563 +       /* Disable soft irqs for various locks below. Also 
1564 +        * stops preemption for RCU. 
1565 +        */
1566 +       local_bh_disable(); 
1567 +
1568 +       /* Updates of qdisc are serialized by queue_lock. 
1569 +        * The struct Qdisc which is pointed to by qdisc is now a 
1570 +        * rcu structure - it may be accessed without acquiring 
1571 +        * a lock (but the structure may be stale.) The freeing of the
1572 +        * qdisc will be deferred until it's known that there are no 
1573 +        * more references to it.
1574 +        * 
1575 +        * If the qdisc has an enqueue function, we still need to 
1576 +        * hold the queue_lock before calling it, since queue_lock
1577 +        * also serializes access to the device queue.
1578 +        */
1579 +
1580 +       q = rcu_dereference(dev->qdisc);
1581 +#ifdef CONFIG_NET_CLS_ACT
1582 +       skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1583 +#endif
1584 +       if (q->enqueue) {
1585 +               /* Grab device queue */
1586 +               spin_lock(&dev->queue_lock);
1587 +
1588 +               rc = q->enqueue(skb, q);
1589 +
1590 +               qdisc_run(dev);
1591 +
1592 +               spin_unlock(&dev->queue_lock);
1593 +               rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1594 +               goto out;
1595 +       }
1596 +
1597 +       /* The device has no queue. Common case for software devices:
1598 +          loopback, all the sorts of tunnels...
1599 +
1600 +          Really, it is unlikely that xmit_lock protection is necessary here.
1601 +          (f.e. loopback and IP tunnels are clean ignoring statistics
1602 +          counters.)
1603 +          However, it is possible, that they rely on protection
1604 +          made by us here.
1605 +
1606 +          Check this and shot the lock. It is not prone from deadlocks.
1607 +          Either shot noqueue qdisc, it is even simpler 8)
1608 +        */
1609 +       if (dev->flags & IFF_UP) {
1610 +               int cpu = smp_processor_id(); /* ok because BHs are off */
1611 +
1612 +               if (dev->xmit_lock_owner != cpu) {
1613 +
1614 +                       HARD_TX_LOCK(dev, cpu);
1615 +
1616 +                       if (!netif_queue_stopped(dev)) {
1617 +                               if (netdev_nit)
1618 +                                       dev_queue_xmit_nit(skb, dev);
1619 +
1620 +                               rc = 0;
1621 +                               if (!dev->hard_start_xmit(skb, dev)) {
1622 +                                       HARD_TX_UNLOCK(dev);
1623 +                                       goto out;
1624 +                               }
1625 +                       }
1626 +                       HARD_TX_UNLOCK(dev);
1627 +                       if (net_ratelimit())
1628 +                               printk(KERN_CRIT "Virtual device %s asks to "
1629 +                                      "queue packet!\n", dev->name);
1630 +               } else {
1631 +                       /* Recursion is detected! It is possible,
1632 +                        * unfortunately */
1633 +                       if (net_ratelimit())
1634 +                               printk(KERN_CRIT "Dead loop on virtual device "
1635 +                                      "%s, fix it urgently!\n", dev->name);
1636 +               }
1637 +       }
1638 +
1639 +       rc = -ENETDOWN;
1640 +       local_bh_enable();
1641 +
1642 +out_kfree_skb:
1643 +       kfree_skb(skb);
1644 +       return rc;
1645 +out:
1646 +       local_bh_enable();
1647 +       return rc;
1648 +}
1649 +
1650 +
1651 +/*=======================================================================
1652 +                       Receiver routines
1653 +  =======================================================================*/
1654 +
1655 +int netdev_max_backlog = 300;
1656 +int weight_p = 64;            /* old backlog weight */
1657 +/* These numbers are selected based on intuition and some
1658 + * experimentatiom, if you have more scientific way of doing this
1659 + * please go ahead and fix things.
1660 + */
1661 +int no_cong_thresh = 10;
1662 +int no_cong = 20;
1663 +int lo_cong = 100;
1664 +int mod_cong = 290;
1665 +
1666 +DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1667 +
1668 +
1669 +static void get_sample_stats(int cpu)
1670 +{
1671 +#ifdef RAND_LIE
1672 +       unsigned long rd;
1673 +       int rq;
1674 +#endif
1675 +       struct softnet_data *sd = &per_cpu(softnet_data, cpu);
1676 +       int blog = sd->input_pkt_queue.qlen;
1677 +       int avg_blog = sd->avg_blog;
1678 +
1679 +       avg_blog = (avg_blog >> 1) + (blog >> 1);
1680 +
1681 +       if (avg_blog > mod_cong) {
1682 +               /* Above moderate congestion levels. */
1683 +               sd->cng_level = NET_RX_CN_HIGH;
1684 +#ifdef RAND_LIE
1685 +               rd = net_random();
1686 +               rq = rd % netdev_max_backlog;
1687 +               if (rq < avg_blog) /* unlucky bastard */
1688 +                       sd->cng_level = NET_RX_DROP;
1689 +#endif
1690 +       } else if (avg_blog > lo_cong) {
1691 +               sd->cng_level = NET_RX_CN_MOD;
1692 +#ifdef RAND_LIE
1693 +               rd = net_random();
1694 +               rq = rd % netdev_max_backlog;
1695 +                       if (rq < avg_blog) /* unlucky bastard */
1696 +                               sd->cng_level = NET_RX_CN_HIGH;
1697 +#endif
1698 +       } else if (avg_blog > no_cong)
1699 +               sd->cng_level = NET_RX_CN_LOW;
1700 +       else  /* no congestion */
1701 +               sd->cng_level = NET_RX_SUCCESS;
1702 +
1703 +       sd->avg_blog = avg_blog;
1704 +}
1705 +
1706 +#ifdef OFFLINE_SAMPLE
1707 +static void sample_queue(unsigned long dummy)
1708 +{
1709 +/* 10 ms 0r 1ms -- i don't care -- JHS */
1710 +       int next_tick = 1;
1711 +       int cpu = smp_processor_id();
1712 +
1713 +       get_sample_stats(cpu);
1714 +       next_tick += jiffies;
1715 +       mod_timer(&samp_timer, next_tick);
1716 +}
1717 +#endif
1718 +
1719 +
1720 +/**
1721 + *     netif_rx        -       post buffer to the network code
1722 + *     @skb: buffer to post
1723 + *
1724 + *     This function receives a packet from a device driver and queues it for
1725 + *     the upper (protocol) levels to process.  It always succeeds. The buffer
1726 + *     may be dropped during processing for congestion control or by the
1727 + *     protocol layers.
1728 + *
1729 + *     return values:
1730 + *     NET_RX_SUCCESS  (no congestion)
1731 + *     NET_RX_CN_LOW   (low congestion)
1732 + *     NET_RX_CN_MOD   (moderate congestion)
1733 + *     NET_RX_CN_HIGH  (high congestion)
1734 + *     NET_RX_DROP     (packet was dropped)
1735 + *
1736 + */
1737 +
1738 +int netif_rx(struct sk_buff *skb)
1739 +{
1740 +       int this_cpu;
1741 +       struct softnet_data *queue;
1742 +       unsigned long flags;
1743 +
1744 +       /* if netpoll wants it, pretend we never saw it */
1745 +       if (netpoll_rx(skb))
1746 +               return NET_RX_DROP;
1747 +
1748 +       if (!skb->stamp.tv_sec)
1749 +               net_timestamp(&skb->stamp);
1750 +
1751 +       /*
1752 +        * The code is rearranged so that the path is the most
1753 +        * short when CPU is congested, but is still operating.
1754 +        */
1755 +       local_irq_save(flags);
1756 +       this_cpu = smp_processor_id();
1757 +       queue = &__get_cpu_var(softnet_data);
1758 +
1759 +       __get_cpu_var(netdev_rx_stat).total++;
1760 +       if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1761 +               if (queue->input_pkt_queue.qlen) {
1762 +                       if (queue->throttle)
1763 +                               goto drop;
1764 +
1765 +enqueue:
1766 +                       dev_hold(skb->dev);
1767 +                       __skb_queue_tail(&queue->input_pkt_queue, skb);
1768 +#ifndef OFFLINE_SAMPLE
1769 +                       get_sample_stats(this_cpu);
1770 +#endif
1771 +                       local_irq_restore(flags);
1772 +                       return queue->cng_level;
1773 +               }
1774 +
1775 +               if (queue->throttle)
1776 +                       queue->throttle = 0;
1777 +
1778 +               netif_rx_schedule(&queue->backlog_dev);
1779 +               goto enqueue;
1780 +       }
1781 +
1782 +       if (!queue->throttle) {
1783 +               queue->throttle = 1;
1784 +               __get_cpu_var(netdev_rx_stat).throttled++;
1785 +       }
1786 +
1787 +drop:
1788 +       __get_cpu_var(netdev_rx_stat).dropped++;
1789 +       local_irq_restore(flags);
1790 +
1791 +       kfree_skb(skb);
1792 +       return NET_RX_DROP;
1793 +}
1794 +
1795 +int netif_rx_ni(struct sk_buff *skb)
1796 +{
1797 +       int err;
1798 +
1799 +       preempt_disable();
1800 +       err = netif_rx(skb);
1801 +       if (local_softirq_pending())
1802 +               do_softirq();
1803 +       preempt_enable();
1804 +
1805 +       return err;
1806 +}
1807 +
1808 +EXPORT_SYMBOL(netif_rx_ni);
1809 +
1810 +static __inline__ void skb_bond(struct sk_buff *skb)
1811 +{
1812 +       struct net_device *dev = skb->dev;
1813 +
1814 +       if (dev->master) {
1815 +               skb->real_dev = skb->dev;
1816 +               skb->dev = dev->master;
1817 +       }
1818 +}
1819 +
1820 +static void net_tx_action(struct softirq_action *h)
1821 +{
1822 +       struct softnet_data *sd = &__get_cpu_var(softnet_data);
1823 +
1824 +       if (sd->completion_queue) {
1825 +               struct sk_buff *clist;
1826 +
1827 +               local_irq_disable();
1828 +               clist = sd->completion_queue;
1829 +               sd->completion_queue = NULL;
1830 +               local_irq_enable();
1831 +
1832 +               while (clist) {
1833 +                       struct sk_buff *skb = clist;
1834 +                       clist = clist->next;
1835 +
1836 +                       BUG_TRAP(!atomic_read(&skb->users));
1837 +                       __kfree_skb(skb);
1838 +               }
1839 +       }
1840 +
1841 +       if (sd->output_queue) {
1842 +               struct net_device *head;
1843 +
1844 +               local_irq_disable();
1845 +               head = sd->output_queue;
1846 +               sd->output_queue = NULL;
1847 +               local_irq_enable();
1848 +
1849 +               while (head) {
1850 +                       struct net_device *dev = head;
1851 +                       head = head->next_sched;
1852 +
1853 +                       smp_mb__before_clear_bit();
1854 +                       clear_bit(__LINK_STATE_SCHED, &dev->state);
1855 +
1856 +                       if (spin_trylock(&dev->queue_lock)) {
1857 +                               qdisc_run(dev);
1858 +                               spin_unlock(&dev->queue_lock);
1859 +                       } else {
1860 +                               netif_schedule(dev);
1861 +                       }
1862 +               }
1863 +       }
1864 +}
1865 +
1866 +static __inline__ int deliver_skb(struct sk_buff *skb,
1867 +                                 struct packet_type *pt_prev)
1868 +{
1869 +       atomic_inc(&skb->users);
1870 +       return pt_prev->func(skb, skb->dev, pt_prev);
1871 +}
1872 +
1873 +#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1874 +int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
1875 +struct net_bridge;
1876 +struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1877 +                                               unsigned char *addr);
1878 +void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
1879 +
1880 +static __inline__ int handle_bridge(struct sk_buff **pskb,
1881 +                                   struct packet_type **pt_prev, int *ret)
1882 +{
1883 +       struct net_bridge_port *port;
1884 +
1885 +       if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
1886 +           (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
1887 +               return 0;
1888 +
1889 +       if (*pt_prev) {
1890 +               *ret = deliver_skb(*pskb, *pt_prev);
1891 +               *pt_prev = NULL;
1892 +       } 
1893 +       
1894 +       return br_handle_frame_hook(port, pskb);
1895 +}
1896 +#else
1897 +#define handle_bridge(skb, pt_prev, ret)       (0)
1898 +#endif
1899 +
1900 +#ifdef CONFIG_NET_CLS_ACT
1901 +/* TODO: Maybe we should just force sch_ingress to be compiled in
1902 + * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1903 + * a compare and 2 stores extra right now if we dont have it on
1904 + * but have CONFIG_NET_CLS_ACT
1905 + * NOTE: This doesnt stop any functionality; if you dont have 
1906 + * the ingress scheduler, you just cant add policies on ingress.
1907 + *
1908 + */
1909 +static int ing_filter(struct sk_buff *skb) 
1910 +{
1911 +       struct Qdisc *q;
1912 +       struct net_device *dev = skb->dev;
1913 +       int result = TC_ACT_OK;
1914 +       
1915 +       if (dev->qdisc_ingress) {
1916 +               __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1917 +               if (MAX_RED_LOOP < ttl++) {
1918 +                       printk("Redir loop detected Dropping packet (%s->%s)\n",
1919 +                               skb->input_dev?skb->input_dev->name:"??",skb->dev->name);
1920 +                       return TC_ACT_SHOT;
1921 +               }
1922 +
1923 +               skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1924 +
1925 +               skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1926 +               if (NULL == skb->input_dev) {
1927 +                       skb->input_dev = skb->dev;
1928 +                       printk("ing_filter:  fixed  %s out %s\n",skb->input_dev->name,skb->dev->name);
1929 +               }
1930 +               spin_lock(&dev->ingress_lock);
1931 +               if ((q = dev->qdisc_ingress) != NULL)
1932 +                       result = q->enqueue(skb, q);
1933 +               spin_unlock(&dev->ingress_lock);
1934 +
1935 +       }
1936 +
1937 +       return result;
1938 +}
1939 +#endif
1940 +
1941 +int netif_receive_skb(struct sk_buff *skb)
1942 +{
1943 +       struct packet_type *ptype, *pt_prev;
1944 +       int ret = NET_RX_DROP;
1945 +       unsigned short type;
1946 +
1947 +       /* if we've gotten here through NAPI, check netpoll */
1948 +       if (skb->dev->poll && netpoll_rx(skb))
1949 +               return NET_RX_DROP;
1950 +
1951 +       if (!skb->stamp.tv_sec)
1952 +               net_timestamp(&skb->stamp);
1953 +
1954 +       skb_bond(skb);
1955 +
1956 +       __get_cpu_var(netdev_rx_stat).total++;
1957 +
1958 +       skb->h.raw = skb->nh.raw = skb->data;
1959 +       skb->mac_len = skb->nh.raw - skb->mac.raw;
1960 +
1961 +       pt_prev = NULL;
1962 +
1963 +       rcu_read_lock();
1964 +
1965 +#ifdef CONFIG_NET_CLS_ACT
1966 +       if (skb->tc_verd & TC_NCLS) {
1967 +               skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1968 +               goto ncls;
1969 +       }
1970 +#endif
1971 +
1972 +       list_for_each_entry_rcu(ptype, &ptype_all, list) {
1973 +               if (!ptype->dev || ptype->dev == skb->dev) {
1974 +                       if (pt_prev) 
1975 +                               ret = deliver_skb(skb, pt_prev);
1976 +                       pt_prev = ptype;
1977 +               }
1978 +       }
1979 +
1980 +#ifdef CONFIG_NET_CLS_ACT
1981 +       if (pt_prev) {
1982 +               ret = deliver_skb(skb, pt_prev);
1983 +               pt_prev = NULL; /* noone else should process this after*/
1984 +       } else {
1985 +               skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1986 +       }
1987 +
1988 +       ret = ing_filter(skb);
1989 +
1990 +       if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1991 +               kfree_skb(skb);
1992 +               goto out;
1993 +       }
1994 +
1995 +       skb->tc_verd = 0;
1996 +ncls:
1997 +#endif
1998 +
1999 +       handle_diverter(skb);
2000 +
2001 +       if (handle_bridge(&skb, &pt_prev, &ret))
2002 +               goto out;
2003 +
2004 +       type = skb->protocol;
2005 +       list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
2006 +               if (ptype->type == type &&
2007 +                   (!ptype->dev || ptype->dev == skb->dev)) {
2008 +                       if (pt_prev) 
2009 +                               ret = deliver_skb(skb, pt_prev);
2010 +                       pt_prev = ptype;
2011 +               }
2012 +       }
2013 +
2014 +       if (pt_prev) {
2015 +               ret = pt_prev->func(skb, skb->dev, pt_prev);
2016 +       } else {
2017 +               kfree_skb(skb);
2018 +               /* Jamal, now you will not able to escape explaining
2019 +                * me how you were going to use this. :-)
2020 +                */
2021 +               ret = NET_RX_DROP;
2022 +       }
2023 +
2024 +out:
2025 +       rcu_read_unlock();
2026 +       return ret;
2027 +}
2028 +
2029 +static int process_backlog(struct net_device *backlog_dev, int *budget)
2030 +{
2031 +       int work = 0;
2032 +       int quota = min(backlog_dev->quota, *budget);
2033 +       struct softnet_data *queue = &__get_cpu_var(softnet_data);
2034 +       unsigned long start_time = jiffies;
2035 +
2036 +       backlog_dev->weight = weight_p;
2037 +       for (;;) {
2038 +               struct sk_buff *skb;
2039 +               struct net_device *dev;
2040 +
2041 +               local_irq_disable();
2042 +               skb = __skb_dequeue(&queue->input_pkt_queue);
2043 +               if (!skb)
2044 +                       goto job_done;
2045 +               local_irq_enable();
2046 +
2047 +               dev = skb->dev;
2048 +
2049 +               netif_receive_skb(skb);
2050 +
2051 +               dev_put(dev);
2052 +
2053 +               work++;
2054 +
2055 +               if (work >= quota || jiffies - start_time > 1)
2056 +                       break;
2057 +
2058 +       }
2059 +
2060 +       backlog_dev->quota -= work;
2061 +       *budget -= work;
2062 +       return -1;
2063 +
2064 +job_done:
2065 +       backlog_dev->quota -= work;
2066 +       *budget -= work;
2067 +
2068 +       list_del(&backlog_dev->poll_list);
2069 +       smp_mb__before_clear_bit();
2070 +       netif_poll_enable(backlog_dev);
2071 +
2072 +       if (queue->throttle)
2073 +               queue->throttle = 0;
2074 +       local_irq_enable();
2075 +       return 0;
2076 +}
2077 +
2078 +static void net_rx_action(struct softirq_action *h)
2079 +{
2080 +       struct softnet_data *queue = &__get_cpu_var(softnet_data);
2081 +       unsigned long start_time = jiffies;
2082 +       int budget = netdev_max_backlog;
2083 +
2084 +       
2085 +       local_irq_disable();
2086 +
2087 +       while (!list_empty(&queue->poll_list)) {
2088 +               struct net_device *dev;
2089 +
2090 +               if (budget <= 0 || jiffies - start_time > 1)
2091 +                       goto softnet_break;
2092 +
2093 +               local_irq_enable();
2094 +
2095 +               dev = list_entry(queue->poll_list.next,
2096 +                                struct net_device, poll_list);
2097 +               netpoll_poll_lock(dev);
2098 +
2099 +               if (dev->quota <= 0 || dev->poll(dev, &budget)) {
2100 +                       netpoll_poll_unlock(dev);
2101 +                       local_irq_disable();
2102 +                       list_del(&dev->poll_list);
2103 +                       list_add_tail(&dev->poll_list, &queue->poll_list);
2104 +                       if (dev->quota < 0)
2105 +                               dev->quota += dev->weight;
2106 +                       else
2107 +                               dev->quota = dev->weight;
2108 +               } else {
2109 +                       netpoll_poll_unlock(dev);
2110 +                       dev_put(dev);
2111 +                       local_irq_disable();
2112 +               }
2113 +       }
2114 +out:
2115 +       local_irq_enable();
2116 +       return;
2117 +
2118 +softnet_break:
2119 +       __get_cpu_var(netdev_rx_stat).time_squeeze++;
2120 +       __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2121 +       goto out;
2122 +}
2123 +
2124 +static gifconf_func_t * gifconf_list [NPROTO];
2125 +
2126 +/**
2127 + *     register_gifconf        -       register a SIOCGIF handler
2128 + *     @family: Address family
2129 + *     @gifconf: Function handler
2130 + *
2131 + *     Register protocol dependent address dumping routines. The handler
2132 + *     that is passed must not be freed or reused until it has been replaced
2133 + *     by another handler.
2134 + */
2135 +int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2136 +{
2137 +       if (family >= NPROTO)
2138 +               return -EINVAL;
2139 +       gifconf_list[family] = gifconf;
2140 +       return 0;
2141 +}
2142 +
2143 +
2144 +/*
2145 + *     Map an interface index to its name (SIOCGIFNAME)
2146 + */
2147 +
2148 +/*
2149 + *     We need this ioctl for efficient implementation of the
2150 + *     if_indextoname() function required by the IPv6 API.  Without
2151 + *     it, we would have to search all the interfaces to find a
2152 + *     match.  --pb
2153 + */
2154 +
2155 +static int dev_ifname(struct ifreq __user *arg)
2156 +{
2157 +       struct net_device *dev;
2158 +       struct ifreq ifr;
2159 +
2160 +       /*
2161 +        *      Fetch the caller's info block.
2162 +        */
2163 +
2164 +       if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2165 +               return -EFAULT;
2166 +
2167 +       read_lock(&dev_base_lock);
2168 +       dev = __dev_get_by_index(ifr.ifr_ifindex);
2169 +       if (!dev) {
2170 +               read_unlock(&dev_base_lock);
2171 +               return -ENODEV;
2172 +       }
2173 +
2174 +       strcpy(ifr.ifr_name, dev->name);
2175 +       read_unlock(&dev_base_lock);
2176 +
2177 +       if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2178 +               return -EFAULT;
2179 +       return 0;
2180 +}
2181 +
2182 +/*
2183 + *     Perform a SIOCGIFCONF call. This structure will change
2184 + *     size eventually, and there is nothing I can do about it.
2185 + *     Thus we will need a 'compatibility mode'.
2186 + */
2187 +
2188 +static int dev_ifconf(char __user *arg)
2189 +{
2190 +       struct ifconf ifc;
2191 +       struct net_device *dev;
2192 +       char __user *pos;
2193 +       int len;
2194 +       int total;
2195 +       int i;
2196 +
2197 +       /*
2198 +        *      Fetch the caller's info block.
2199 +        */
2200 +
2201 +       if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2202 +               return -EFAULT;
2203 +
2204 +       pos = ifc.ifc_buf;
2205 +       len = ifc.ifc_len;
2206 +
2207 +       /*
2208 +        *      Loop over the interfaces, and write an info block for each.
2209 +        */
2210 +
2211 +       total = 0;
2212 +       for (dev = dev_base; dev; dev = dev->next) {
2213 +               for (i = 0; i < NPROTO; i++) {
2214 +                       if (gifconf_list[i]) {
2215 +                               int done;
2216 +                               if (!pos)
2217 +                                       done = gifconf_list[i](dev, NULL, 0);
2218 +                               else
2219 +                                       done = gifconf_list[i](dev, pos + total,
2220 +                                                              len - total);
2221 +                               if (done < 0)
2222 +                                       return -EFAULT;
2223 +                               total += done;
2224 +                       }
2225 +               }
2226 +       }
2227 +
2228 +       /*
2229 +        *      All done.  Write the updated control block back to the caller.
2230 +        */
2231 +       ifc.ifc_len = total;
2232 +
2233 +       /*
2234 +        *      Both BSD and Solaris return 0 here, so we do too.
2235 +        */
2236 +       return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2237 +}
2238 +
2239 +#ifdef CONFIG_PROC_FS
2240 +/*
2241 + *     This is invoked by the /proc filesystem handler to display a device
2242 + *     in detail.
2243 + */
2244 +static __inline__ struct net_device *dev_get_idx(loff_t pos)
2245 +{
2246 +       struct net_device *dev;
2247 +       loff_t i;
2248 +
2249 +       for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
2250 +
2251 +       return i == pos ? dev : NULL;
2252 +}
2253 +
2254 +void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2255 +{
2256 +       read_lock(&dev_base_lock);
2257 +       return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
2258 +}
2259 +
2260 +void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2261 +{
2262 +       ++*pos;
2263 +       return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
2264 +}
2265 +
2266 +void dev_seq_stop(struct seq_file *seq, void *v)
2267 +{
2268 +       read_unlock(&dev_base_lock);
2269 +}
2270 +
2271 +static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2272 +{
2273 +       if (dev->get_stats) {
2274 +               struct net_device_stats *stats = dev->get_stats(dev);
2275 +
2276 +               seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2277 +                               "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2278 +                          dev->name, stats->rx_bytes, stats->rx_packets,
2279 +                          stats->rx_errors,
2280 +                          stats->rx_dropped + stats->rx_missed_errors,
2281 +                          stats->rx_fifo_errors,
2282 +                          stats->rx_length_errors + stats->rx_over_errors +
2283 +                            stats->rx_crc_errors + stats->rx_frame_errors,
2284 +                          stats->rx_compressed, stats->multicast,
2285 +                          stats->tx_bytes, stats->tx_packets,
2286 +                          stats->tx_errors, stats->tx_dropped,
2287 +                          stats->tx_fifo_errors, stats->collisions,
2288 +                          stats->tx_carrier_errors +
2289 +                            stats->tx_aborted_errors +
2290 +                            stats->tx_window_errors +
2291 +                            stats->tx_heartbeat_errors,
2292 +                          stats->tx_compressed);
2293 +       } else
2294 +               seq_printf(seq, "%6s: No statistics available.\n", dev->name);
2295 +}
2296 +
2297 +/*
2298 + *     Called from the PROCfs module. This now uses the new arbitrary sized
2299 + *     /proc/net interface to create /proc/net/dev
2300 + */
2301 +static int dev_seq_show(struct seq_file *seq, void *v)
2302 +{
2303 +       if (v == SEQ_START_TOKEN)
2304 +               seq_puts(seq, "Inter-|   Receive                            "
2305 +                             "                    |  Transmit\n"
2306 +                             " face |bytes    packets errs drop fifo frame "
2307 +                             "compressed multicast|bytes    packets errs "
2308 +                             "drop fifo colls carrier compressed\n");
2309 +       else
2310 +               dev_seq_printf_stats(seq, v);
2311 +       return 0;
2312 +}
2313 +
2314 +static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2315 +{
2316 +       struct netif_rx_stats *rc = NULL;
2317 +
2318 +       while (*pos < NR_CPUS)
2319 +               if (cpu_online(*pos)) {
2320 +                       rc = &per_cpu(netdev_rx_stat, *pos);
2321 +                       break;
2322 +               } else
2323 +                       ++*pos;
2324 +       return rc;
2325 +}
2326 +
2327 +static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2328 +{
2329 +       return softnet_get_online(pos);
2330 +}
2331 +
2332 +static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2333 +{
2334 +       ++*pos;
2335 +       return softnet_get_online(pos);
2336 +}
2337 +
2338 +static void softnet_seq_stop(struct seq_file *seq, void *v)
2339 +{
2340 +}
2341 +
2342 +static int softnet_seq_show(struct seq_file *seq, void *v)
2343 +{
2344 +       struct netif_rx_stats *s = v;
2345 +
2346 +       seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2347 +                  s->total, s->dropped, s->time_squeeze, s->throttled,
2348 +                  s->fastroute_hit, s->fastroute_success, s->fastroute_defer,
2349 +                  s->fastroute_deferred_out,
2350 +#if 0
2351 +                  s->fastroute_latency_reduction
2352 +#else
2353 +                  s->cpu_collision
2354 +#endif
2355 +                 );
2356 +       return 0;
2357 +}
2358 +
2359 +static struct seq_operations dev_seq_ops = {
2360 +       .start = dev_seq_start,
2361 +       .next  = dev_seq_next,
2362 +       .stop  = dev_seq_stop,
2363 +       .show  = dev_seq_show,
2364 +};
2365 +
2366 +static int dev_seq_open(struct inode *inode, struct file *file)
2367 +{
2368 +       return seq_open(file, &dev_seq_ops);
2369 +}
2370 +
2371 +static struct file_operations dev_seq_fops = {
2372 +       .owner   = THIS_MODULE,
2373 +       .open    = dev_seq_open,
2374 +       .read    = seq_read,
2375 +       .llseek  = seq_lseek,
2376 +       .release = seq_release,
2377 +};
2378 +
2379 +static struct seq_operations softnet_seq_ops = {
2380 +       .start = softnet_seq_start,
2381 +       .next  = softnet_seq_next,
2382 +       .stop  = softnet_seq_stop,
2383 +       .show  = softnet_seq_show,
2384 +};
2385 +
2386 +static int softnet_seq_open(struct inode *inode, struct file *file)
2387 +{
2388 +       return seq_open(file, &softnet_seq_ops);
2389 +}
2390 +
2391 +static struct file_operations softnet_seq_fops = {
2392 +       .owner   = THIS_MODULE,
2393 +       .open    = softnet_seq_open,
2394 +       .read    = seq_read,
2395 +       .llseek  = seq_lseek,
2396 +       .release = seq_release,
2397 +};
2398 +
2399 +#ifdef WIRELESS_EXT
2400 +extern int wireless_proc_init(void);
2401 +#else
2402 +#define wireless_proc_init() 0
2403 +#endif
2404 +
2405 +static int __init dev_proc_init(void)
2406 +{
2407 +       int rc = -ENOMEM;
2408 +
2409 +       if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2410 +               goto out;
2411 +       if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2412 +               goto out_dev;
2413 +       if (wireless_proc_init())
2414 +               goto out_softnet;
2415 +       rc = 0;
2416 +out:
2417 +       return rc;
2418 +out_softnet:
2419 +       proc_net_remove("softnet_stat");
2420 +out_dev:
2421 +       proc_net_remove("dev");
2422 +       goto out;
2423 +}
2424 +#else
2425 +#define dev_proc_init() 0
2426 +#endif /* CONFIG_PROC_FS */
2427 +
2428 +
2429 +/**
2430 + *     netdev_set_master       -       set up master/slave pair
2431 + *     @slave: slave device
2432 + *     @master: new master device
2433 + *
2434 + *     Changes the master device of the slave. Pass %NULL to break the
2435 + *     bonding. The caller must hold the RTNL semaphore. On a failure
2436 + *     a negative errno code is returned. On success the reference counts
2437 + *     are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2438 + *     function returns zero.
2439 + */
2440 +int netdev_set_master(struct net_device *slave, struct net_device *master)
2441 +{
2442 +       struct net_device *old = slave->master;
2443 +
2444 +       ASSERT_RTNL();
2445 +
2446 +       if (master) {
2447 +               if (old)
2448 +                       return -EBUSY;
2449 +               dev_hold(master);
2450 +       }
2451 +
2452 +       slave->master = master;
2453 +       
2454 +       synchronize_net();
2455 +
2456 +       if (old)
2457 +               dev_put(old);
2458 +
2459 +       if (master)
2460 +               slave->flags |= IFF_SLAVE;
2461 +       else
2462 +               slave->flags &= ~IFF_SLAVE;
2463 +
2464 +       rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2465 +       return 0;
2466 +}
2467 +
2468 +/**
2469 + *     dev_set_promiscuity     - update promiscuity count on a device
2470 + *     @dev: device
2471 + *     @inc: modifier
2472 + *
2473 + *     Add or remove promsicuity from a device. While the count in the device
2474 + *     remains above zero the interface remains promiscuous. Once it hits zero
2475 + *     the device reverts back to normal filtering operation. A negative inc
2476 + *     value is used to drop promiscuity on the device.
2477 + */
2478 +void dev_set_promiscuity(struct net_device *dev, int inc)
2479 +{
2480 +       unsigned short old_flags = dev->flags;
2481 +
2482 +       dev->flags |= IFF_PROMISC;
2483 +       if ((dev->promiscuity += inc) == 0)
2484 +               dev->flags &= ~IFF_PROMISC;
2485 +       if (dev->flags ^ old_flags) {
2486 +               dev_mc_upload(dev);
2487 +               printk(KERN_INFO "device %s %s promiscuous mode\n",
2488 +                      dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2489 +                                                              "left");
2490 +       }
2491 +}
2492 +
2493 +/**
2494 + *     dev_set_allmulti        - update allmulti count on a device
2495 + *     @dev: device
2496 + *     @inc: modifier
2497 + *
2498 + *     Add or remove reception of all multicast frames to a device. While the
2499 + *     count in the device remains above zero the interface remains listening
2500 + *     to all interfaces. Once it hits zero the device reverts back to normal
2501 + *     filtering operation. A negative @inc value is used to drop the counter
2502 + *     when releasing a resource needing all multicasts.
2503 + */
2504 +
2505 +void dev_set_allmulti(struct net_device *dev, int inc)
2506 +{
2507 +       unsigned short old_flags = dev->flags;
2508 +
2509 +       dev->flags |= IFF_ALLMULTI;
2510 +       if ((dev->allmulti += inc) == 0)
2511 +               dev->flags &= ~IFF_ALLMULTI;
2512 +       if (dev->flags ^ old_flags)
2513 +               dev_mc_upload(dev);
2514 +}
2515 +
2516 +unsigned dev_get_flags(const struct net_device *dev)
2517 +{
2518 +       unsigned flags;
2519 +
2520 +       flags = (dev->flags & ~(IFF_PROMISC |
2521 +                               IFF_ALLMULTI |
2522 +                               IFF_RUNNING)) | 
2523 +               (dev->gflags & (IFF_PROMISC |
2524 +                               IFF_ALLMULTI));
2525 +
2526 +       if (netif_running(dev) && netif_carrier_ok(dev))
2527 +               flags |= IFF_RUNNING;
2528 +
2529 +       return flags;
2530 +}
2531 +
2532 +int dev_change_flags(struct net_device *dev, unsigned flags)
2533 +{
2534 +       int ret;
2535 +       int old_flags = dev->flags;
2536 +
2537 +       /*
2538 +        *      Set the flags on our device.
2539 +        */
2540 +
2541 +       dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2542 +                              IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2543 +                              IFF_AUTOMEDIA)) |
2544 +                    (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2545 +                                   IFF_ALLMULTI));
2546 +
2547 +       /*
2548 +        *      Load in the correct multicast list now the flags have changed.
2549 +        */
2550 +
2551 +       dev_mc_upload(dev);
2552 +
2553 +       /*
2554 +        *      Have we downed the interface. We handle IFF_UP ourselves
2555 +        *      according to user attempts to set it, rather than blindly
2556 +        *      setting it.
2557 +        */
2558 +
2559 +       ret = 0;
2560 +       if ((old_flags ^ flags) & IFF_UP) {     /* Bit is different  ? */
2561 +               ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2562 +
2563 +               if (!ret)
2564 +                       dev_mc_upload(dev);
2565 +       }
2566 +
2567 +       if (dev->flags & IFF_UP &&
2568 +           ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2569 +                                         IFF_VOLATILE)))
2570 +               notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
2571 +
2572 +       if ((flags ^ dev->gflags) & IFF_PROMISC) {
2573 +               int inc = (flags & IFF_PROMISC) ? +1 : -1;
2574 +               dev->gflags ^= IFF_PROMISC;
2575 +               dev_set_promiscuity(dev, inc);
2576 +       }
2577 +
2578 +       /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2579 +          is important. Some (broken) drivers set IFF_PROMISC, when
2580 +          IFF_ALLMULTI is requested not asking us and not reporting.
2581 +        */
2582 +       if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2583 +               int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2584 +               dev->gflags ^= IFF_ALLMULTI;
2585 +               dev_set_allmulti(dev, inc);
2586 +       }
2587 +
2588 +       if (old_flags ^ dev->flags)
2589 +               rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
2590 +
2591 +       return ret;
2592 +}
2593 +
2594 +int dev_set_mtu(struct net_device *dev, int new_mtu)
2595 +{
2596 +       int err;
2597 +
2598 +       if (new_mtu == dev->mtu)
2599 +               return 0;
2600 +
2601 +       /*      MTU must be positive.    */
2602 +       if (new_mtu < 0)
2603 +               return -EINVAL;
2604 +
2605 +       if (!netif_device_present(dev))
2606 +               return -ENODEV;
2607 +
2608 +       err = 0;
2609 +       if (dev->change_mtu)
2610 +               err = dev->change_mtu(dev, new_mtu);
2611 +       else
2612 +               dev->mtu = new_mtu;
2613 +       if (!err && dev->flags & IFF_UP)
2614 +               notifier_call_chain(&netdev_chain,
2615 +                                   NETDEV_CHANGEMTU, dev);
2616 +       return err;
2617 +}
2618 +
2619 +int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2620 +{
2621 +       int err;
2622 +
2623 +       if (!dev->set_mac_address)
2624 +               return -EOPNOTSUPP;
2625 +       if (sa->sa_family != dev->type)
2626 +               return -EINVAL;
2627 +       if (!netif_device_present(dev))
2628 +               return -ENODEV;
2629 +       err = dev->set_mac_address(dev, sa);
2630 +       if (!err)
2631 +               notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
2632 +       return err;
2633 +}
2634 +
2635 +/*
2636 + *     Perform the SIOCxIFxxx calls.
2637 + */
2638 +static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2639 +{
2640 +       int err;
2641 +       struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2642 +
2643 +       if (!dev)
2644 +               return -ENODEV;
2645 +
2646 +       switch (cmd) {
2647 +               case SIOCGIFFLAGS:      /* Get interface flags */
2648 +                       ifr->ifr_flags = dev_get_flags(dev);
2649 +                       return 0;
2650 +
2651 +               case SIOCSIFFLAGS:      /* Set interface flags */
2652 +                       return dev_change_flags(dev, ifr->ifr_flags);
2653 +
2654 +               case SIOCGIFMETRIC:     /* Get the metric on the interface
2655 +                                          (currently unused) */
2656 +                       ifr->ifr_metric = 0;
2657 +                       return 0;
2658 +
2659 +               case SIOCSIFMETRIC:     /* Set the metric on the interface
2660 +                                          (currently unused) */
2661 +                       return -EOPNOTSUPP;
2662 +
2663 +               case SIOCGIFMTU:        /* Get the MTU of a device */
2664 +                       ifr->ifr_mtu = dev->mtu;
2665 +                       return 0;
2666 +
2667 +               case SIOCSIFMTU:        /* Set the MTU of a device */
2668 +                       return dev_set_mtu(dev, ifr->ifr_mtu);
2669 +
2670 +               case SIOCGIFHWADDR:
2671 +                       if (!dev->addr_len)
2672 +                               memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2673 +                       else
2674 +                               memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2675 +                                      min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2676 +                       ifr->ifr_hwaddr.sa_family = dev->type;
2677 +                       return 0;
2678 +
2679 +               case SIOCSIFHWADDR:
2680 +                       return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2681 +
2682 +               case SIOCSIFHWBROADCAST:
2683 +                       if (ifr->ifr_hwaddr.sa_family != dev->type)
2684 +                               return -EINVAL;
2685 +                       memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2686 +                              min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2687 +                       notifier_call_chain(&netdev_chain,
2688 +                                           NETDEV_CHANGEADDR, dev);
2689 +                       return 0;
2690 +
2691 +               case SIOCGIFMAP:
2692 +                       ifr->ifr_map.mem_start = dev->mem_start;
2693 +                       ifr->ifr_map.mem_end   = dev->mem_end;
2694 +                       ifr->ifr_map.base_addr = dev->base_addr;
2695 +                       ifr->ifr_map.irq       = dev->irq;
2696 +                       ifr->ifr_map.dma       = dev->dma;
2697 +                       ifr->ifr_map.port      = dev->if_port;
2698 +                       return 0;
2699 +
2700 +               case SIOCSIFMAP:
2701 +                       if (dev->set_config) {
2702 +                               if (!netif_device_present(dev))
2703 +                                       return -ENODEV;
2704 +                               return dev->set_config(dev, &ifr->ifr_map);
2705 +                       }
2706 +                       return -EOPNOTSUPP;
2707 +
2708 +               case SIOCADDMULTI:
2709 +                       if (!dev->set_multicast_list ||
2710 +                           ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2711 +                               return -EINVAL;
2712 +                       if (!netif_device_present(dev))
2713 +                               return -ENODEV;
2714 +                       return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2715 +                                         dev->addr_len, 1);
2716 +
2717 +               case SIOCDELMULTI:
2718 +                       if (!dev->set_multicast_list ||
2719 +                           ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2720 +                               return -EINVAL;
2721 +                       if (!netif_device_present(dev))
2722 +                               return -ENODEV;
2723 +                       return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2724 +                                            dev->addr_len, 1);
2725 +
2726 +               case SIOCGIFINDEX:
2727 +                       ifr->ifr_ifindex = dev->ifindex;
2728 +                       return 0;
2729 +
2730 +               case SIOCGIFTXQLEN:
2731 +                       ifr->ifr_qlen = dev->tx_queue_len;
2732 +                       return 0;
2733 +
2734 +               case SIOCSIFTXQLEN:
2735 +                       if (ifr->ifr_qlen < 0)
2736 +                               return -EINVAL;
2737 +                       dev->tx_queue_len = ifr->ifr_qlen;
2738 +                       return 0;
2739 +
2740 +               case SIOCSIFNAME:
2741 +                       ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2742 +                       return dev_change_name(dev, ifr->ifr_newname);
2743 +
2744 +               /*
2745 +                *      Unknown or private ioctl
2746 +                */
2747 +
2748 +               default:
2749 +                       if ((cmd >= SIOCDEVPRIVATE &&
2750 +                           cmd <= SIOCDEVPRIVATE + 15) ||
2751 +                           cmd == SIOCBONDENSLAVE ||
2752 +                           cmd == SIOCBONDRELEASE ||
2753 +                           cmd == SIOCBONDSETHWADDR ||
2754 +                           cmd == SIOCBONDSLAVEINFOQUERY ||
2755 +                           cmd == SIOCBONDINFOQUERY ||
2756 +                           cmd == SIOCBONDCHANGEACTIVE ||
2757 +                           cmd == SIOCGMIIPHY ||
2758 +                           cmd == SIOCGMIIREG ||
2759 +                           cmd == SIOCSMIIREG ||
2760 +                           cmd == SIOCBRADDIF ||
2761 +                           cmd == SIOCBRDELIF ||
2762 +                           cmd == SIOCWANDEV) {
2763 +                               err = -EOPNOTSUPP;
2764 +                               if (dev->do_ioctl) {
2765 +                                       if (netif_device_present(dev))
2766 +                                               err = dev->do_ioctl(dev, ifr,
2767 +                                                                   cmd);
2768 +                                       else
2769 +                                               err = -ENODEV;
2770 +                               }
2771 +                       } else
2772 +                               err = -EINVAL;
2773 +
2774 +       }
2775 +       return err;
2776 +}
2777 +
2778 +/*
2779 + *     This function handles all "interface"-type I/O control requests. The actual
2780 + *     'doing' part of this is dev_ifsioc above.
2781 + */
2782 +
2783 +/**
2784 + *     dev_ioctl       -       network device ioctl
2785 + *     @cmd: command to issue
2786 + *     @arg: pointer to a struct ifreq in user space
2787 + *
2788 + *     Issue ioctl functions to devices. This is normally called by the
2789 + *     user space syscall interfaces but can sometimes be useful for
2790 + *     other purposes. The return value is the return from the syscall if
2791 + *     positive or a negative errno code on error.
2792 + */
2793 +
2794 +int dev_ioctl(unsigned int cmd, void __user *arg)
2795 +{
2796 +       struct ifreq ifr;
2797 +       int ret;
2798 +       char *colon;
2799 +
2800 +       /* One special case: SIOCGIFCONF takes ifconf argument
2801 +          and requires shared lock, because it sleeps writing
2802 +          to user space.
2803 +        */
2804 +
2805 +       if (cmd == SIOCGIFCONF) {
2806 +               rtnl_shlock();
2807 +               ret = dev_ifconf((char __user *) arg);
2808 +               rtnl_shunlock();
2809 +               return ret;
2810 +       }
2811 +       if (cmd == SIOCGIFNAME)
2812 +               return dev_ifname((struct ifreq __user *)arg);
2813 +
2814 +       if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2815 +               return -EFAULT;
2816 +
2817 +       ifr.ifr_name[IFNAMSIZ-1] = 0;
2818 +
2819 +       colon = strchr(ifr.ifr_name, ':');
2820 +       if (colon)
2821 +               *colon = 0;
2822 +
2823 +       /*
2824 +        *      See which interface the caller is talking about.
2825 +        */
2826 +
2827 +       switch (cmd) {
2828 +               /*
2829 +                *      These ioctl calls:
2830 +                *      - can be done by all.
2831 +                *      - atomic and do not require locking.
2832 +                *      - return a value
2833 +                */
2834 +               case SIOCGIFFLAGS:
2835 +               case SIOCGIFMETRIC:
2836 +               case SIOCGIFMTU:
2837 +               case SIOCGIFHWADDR:
2838 +               case SIOCGIFSLAVE:
2839 +               case SIOCGIFMAP:
2840 +               case SIOCGIFINDEX:
2841 +               case SIOCGIFTXQLEN:
2842 +                       dev_load(ifr.ifr_name);
2843 +                       read_lock(&dev_base_lock);
2844 +                       ret = dev_ifsioc(&ifr, cmd);
2845 +                       read_unlock(&dev_base_lock);
2846 +                       if (!ret) {
2847 +                               if (colon)
2848 +                                       *colon = ':';
2849 +                               if (copy_to_user(arg, &ifr,
2850 +                                                sizeof(struct ifreq)))
2851 +                                       ret = -EFAULT;
2852 +                       }
2853 +                       return ret;
2854 +
2855 +               case SIOCETHTOOL:
2856 +                       dev_load(ifr.ifr_name);
2857 +                       rtnl_lock();
2858 +                       ret = dev_ethtool(&ifr);
2859 +                       rtnl_unlock();
2860 +                       if (!ret) {
2861 +                               if (colon)
2862 +                                       *colon = ':';
2863 +                               if (copy_to_user(arg, &ifr,
2864 +                                                sizeof(struct ifreq)))
2865 +                                       ret = -EFAULT;
2866 +                       }
2867 +                       return ret;
2868 +
2869 +               /*
2870 +                *      These ioctl calls:
2871 +                *      - require superuser power.
2872 +                *      - require strict serialization.
2873 +                *      - return a value
2874 +                */
2875 +               case SIOCGMIIPHY:
2876 +               case SIOCGMIIREG:
2877 +               case SIOCSIFNAME:
2878 +                       if (!capable(CAP_NET_ADMIN))
2879 +                               return -EPERM;
2880 +                       dev_load(ifr.ifr_name);
2881 +                       rtnl_lock();
2882 +                       ret = dev_ifsioc(&ifr, cmd);
2883 +                       rtnl_unlock();
2884 +                       if (!ret) {
2885 +                               if (colon)
2886 +                                       *colon = ':';
2887 +                               if (copy_to_user(arg, &ifr,
2888 +                                                sizeof(struct ifreq)))
2889 +                                       ret = -EFAULT;
2890 +                       }
2891 +                       return ret;
2892 +
2893 +               /*
2894 +                *      These ioctl calls:
2895 +                *      - require superuser power.
2896 +                *      - require strict serialization.
2897 +                *      - do not return a value
2898 +                */
2899 +               case SIOCSIFFLAGS:
2900 +               case SIOCSIFMETRIC:
2901 +               case SIOCSIFMTU:
2902 +               case SIOCSIFMAP:
2903 +               case SIOCSIFHWADDR:
2904 +               case SIOCSIFSLAVE:
2905 +               case SIOCADDMULTI:
2906 +               case SIOCDELMULTI:
2907 +               case SIOCSIFHWBROADCAST:
2908 +               case SIOCSIFTXQLEN:
2909 +               case SIOCSMIIREG:
2910 +               case SIOCBONDENSLAVE:
2911 +               case SIOCBONDRELEASE:
2912 +               case SIOCBONDSETHWADDR:
2913 +               case SIOCBONDSLAVEINFOQUERY:
2914 +               case SIOCBONDINFOQUERY:
2915 +               case SIOCBONDCHANGEACTIVE:
2916 +               case SIOCBRADDIF:
2917 +               case SIOCBRDELIF:
2918 +                       if (!capable(CAP_NET_ADMIN))
2919 +                               return -EPERM;
2920 +                       dev_load(ifr.ifr_name);
2921 +                       rtnl_lock();
2922 +                       ret = dev_ifsioc(&ifr, cmd);
2923 +                       rtnl_unlock();
2924 +                       return ret;
2925 +
2926 +               case SIOCGIFMEM:
2927 +                       /* Get the per device memory space. We can add this but
2928 +                        * currently do not support it */
2929 +               case SIOCSIFMEM:
2930 +                       /* Set the per device memory buffer space.
2931 +                        * Not applicable in our case */
2932 +               case SIOCSIFLINK:
2933 +                       return -EINVAL;
2934 +
2935 +               /*
2936 +                *      Unknown or private ioctl.
2937 +                */
2938 +               default:
2939 +                       if (cmd == SIOCWANDEV ||
2940 +                           (cmd >= SIOCDEVPRIVATE &&
2941 +                            cmd <= SIOCDEVPRIVATE + 15)) {
2942 +                               dev_load(ifr.ifr_name);
2943 +                               rtnl_lock();
2944 +                               ret = dev_ifsioc(&ifr, cmd);
2945 +                               rtnl_unlock();
2946 +                               if (!ret && copy_to_user(arg, &ifr,
2947 +                                                        sizeof(struct ifreq)))
2948 +                                       ret = -EFAULT;
2949 +                               return ret;
2950 +                       }
2951 +#ifdef WIRELESS_EXT
2952 +                       /* Take care of Wireless Extensions */
2953 +                       if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2954 +                               /* If command is `set a parameter', or
2955 +                                * `get the encoding parameters', check if
2956 +                                * the user has the right to do it */
2957 +                               if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE) {
2958 +                                       if (!capable(CAP_NET_ADMIN))
2959 +                                               return -EPERM;
2960 +                               }
2961 +                               dev_load(ifr.ifr_name);
2962 +                               rtnl_lock();
2963 +                               /* Follow me in net/core/wireless.c */
2964 +                               ret = wireless_process_ioctl(&ifr, cmd);
2965 +                               rtnl_unlock();
2966 +                               if (IW_IS_GET(cmd) &&
2967 +                                   copy_to_user(arg, &ifr,
2968 +                                                sizeof(struct ifreq)))
2969 +                                       ret = -EFAULT;
2970 +                               return ret;
2971 +                       }
2972 +#endif /* WIRELESS_EXT */
2973 +                       return -EINVAL;
2974 +       }
2975 +}
2976 +
2977 +
2978 +/**
2979 + *     dev_new_index   -       allocate an ifindex
2980 + *
2981 + *     Returns a suitable unique value for a new device interface
2982 + *     number.  The caller must hold the rtnl semaphore or the
2983 + *     dev_base_lock to be sure it remains unique.
2984 + */
2985 +static int dev_new_index(void)
2986 +{
2987 +       static int ifindex;
2988 +       for (;;) {
2989 +               if (++ifindex <= 0)
2990 +                       ifindex = 1;
2991 +               if (!__dev_get_by_index(ifindex))
2992 +                       return ifindex;
2993 +       }
2994 +}
2995 +
2996 +static int dev_boot_phase = 1;
2997 +
2998 +/* Delayed registration/unregisteration */
2999 +static DEFINE_SPINLOCK(net_todo_list_lock);
3000 +static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
3001 +
3002 +static inline void net_set_todo(struct net_device *dev)
3003 +{
3004 +       spin_lock(&net_todo_list_lock);
3005 +       list_add_tail(&dev->todo_list, &net_todo_list);
3006 +       spin_unlock(&net_todo_list_lock);
3007 +}
3008 +
3009 +/**
3010 + *     register_netdevice      - register a network device
3011 + *     @dev: device to register
3012 + *
3013 + *     Take a completed network device structure and add it to the kernel
3014 + *     interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3015 + *     chain. 0 is returned on success. A negative errno code is returned
3016 + *     on a failure to set up the device, or if the name is a duplicate.
3017 + *
3018 + *     Callers must hold the rtnl semaphore. You may want
3019 + *     register_netdev() instead of this.
3020 + *
3021 + *     BUGS:
3022 + *     The locking appears insufficient to guarantee two parallel registers
3023 + *     will not get the same name.
3024 + */
3025 +
3026 +int register_netdevice(struct net_device *dev)
3027 +{
3028 +       struct hlist_head *head;
3029 +       struct hlist_node *p;
3030 +       int ret;
3031 +
3032 +       BUG_ON(dev_boot_phase);
3033 +       ASSERT_RTNL();
3034 +
3035 +       /* When net_device's are persistent, this will be fatal. */
3036 +       BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
3037 +
3038 +       spin_lock_init(&dev->queue_lock);
3039 +       spin_lock_init(&dev->xmit_lock);
3040 +       dev->xmit_lock_owner = -1;
3041 +#ifdef CONFIG_NET_CLS_ACT
3042 +       spin_lock_init(&dev->ingress_lock);
3043 +#endif
3044 +
3045 +       ret = alloc_divert_blk(dev);
3046 +       if (ret)
3047 +               goto out;
3048 +
3049 +       dev->iflink = -1;
3050 +
3051 +       /* Init, if this function is available */
3052 +       if (dev->init) {
3053 +               ret = dev->init(dev);
3054 +               if (ret) {
3055 +                       if (ret > 0)
3056 +                               ret = -EIO;
3057 +                       goto out_err;
3058 +               }
3059 +       }
3060
3061 +       if (!dev_valid_name(dev->name)) {
3062 +               ret = -EINVAL;
3063 +               goto out_err;
3064 +       }
3065 +
3066 +       dev->ifindex = dev_new_index();
3067 +       if (dev->iflink == -1)
3068 +               dev->iflink = dev->ifindex;
3069 +
3070 +       /* Check for existence of name */
3071 +       head = dev_name_hash(dev->name);
3072 +       hlist_for_each(p, head) {
3073 +               struct net_device *d
3074 +                       = hlist_entry(p, struct net_device, name_hlist);
3075 +               if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3076 +                       ret = -EEXIST;
3077 +                       goto out_err;
3078 +               }
3079 +       }
3080 +
3081 +       /* Fix illegal SG+CSUM combinations. */
3082 +       if ((dev->features & NETIF_F_SG) &&
3083 +           !(dev->features & (NETIF_F_IP_CSUM |
3084 +                              NETIF_F_NO_CSUM |
3085 +                              NETIF_F_HW_CSUM))) {
3086 +               printk("%s: Dropping NETIF_F_SG since no checksum feature.\n",
3087 +                      dev->name);
3088 +               dev->features &= ~NETIF_F_SG;
3089 +       }
3090 +
3091 +       /* TSO requires that SG is present as well. */
3092 +       if ((dev->features & NETIF_F_TSO) &&
3093 +           !(dev->features & NETIF_F_SG)) {
3094 +               printk("%s: Dropping NETIF_F_TSO since no SG feature.\n",
3095 +                      dev->name);
3096 +               dev->features &= ~NETIF_F_TSO;
3097 +       }
3098 +
3099 +       /*
3100 +        *      nil rebuild_header routine,
3101 +        *      that should be never called and used as just bug trap.
3102 +        */
3103 +
3104 +       if (!dev->rebuild_header)
3105 +               dev->rebuild_header = default_rebuild_header;
3106 +
3107 +       /*
3108 +        *      Default initial state at registry is that the
3109 +        *      device is present.
3110 +        */
3111 +
3112 +       set_bit(__LINK_STATE_PRESENT, &dev->state);
3113 +
3114 +       dev->next = NULL;
3115 +       dev_init_scheduler(dev);
3116 +       write_lock_bh(&dev_base_lock);
3117 +       *dev_tail = dev;
3118 +       dev_tail = &dev->next;
3119 +       hlist_add_head(&dev->name_hlist, head);
3120 +       hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
3121 +       dev_hold(dev);
3122 +       dev->reg_state = NETREG_REGISTERING;
3123 +       write_unlock_bh(&dev_base_lock);
3124 +
3125 +       /* Notify protocols, that a new device appeared. */
3126 +       notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
3127 +
3128 +       /* Finish registration after unlock */
3129 +       net_set_todo(dev);
3130 +       ret = 0;
3131 +
3132 +out:
3133 +       return ret;
3134 +out_err:
3135 +       free_divert_blk(dev);
3136 +       goto out;
3137 +}
3138 +
3139 +/**
3140 + *     register_netdev - register a network device
3141 + *     @dev: device to register
3142 + *
3143 + *     Take a completed network device structure and add it to the kernel
3144 + *     interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3145 + *     chain. 0 is returned on success. A negative errno code is returned
3146 + *     on a failure to set up the device, or if the name is a duplicate.
3147 + *
3148 + *     This is a wrapper around register_netdev that takes the rtnl semaphore
3149 + *     and expands the device name if you passed a format string to
3150 + *     alloc_netdev.
3151 + */
3152 +int register_netdev(struct net_device *dev)
3153 +{
3154 +       int err;
3155 +
3156 +       rtnl_lock();
3157 +
3158 +       /*
3159 +        * If the name is a format string the caller wants us to do a
3160 +        * name allocation.
3161 +        */
3162 +       if (strchr(dev->name, '%')) {
3163 +               err = dev_alloc_name(dev, dev->name);
3164 +               if (err < 0)
3165 +                       goto out;
3166 +       }
3167 +       
3168 +       /*
3169 +        * Back compatibility hook. Kill this one in 2.5
3170 +        */
3171 +       if (dev->name[0] == 0 || dev->name[0] == ' ') {
3172 +               err = dev_alloc_name(dev, "eth%d");
3173 +               if (err < 0)
3174 +                       goto out;
3175 +       }
3176 +
3177 +       err = register_netdevice(dev);
3178 +out:
3179 +       rtnl_unlock();
3180 +       return err;
3181 +}
3182 +EXPORT_SYMBOL(register_netdev);
3183 +
3184 +/*
3185 + * netdev_wait_allrefs - wait until all references are gone.
3186 + *
3187 + * This is called when unregistering network devices.
3188 + *
3189 + * Any protocol or device that holds a reference should register
3190 + * for netdevice notification, and cleanup and put back the
3191 + * reference if they receive an UNREGISTER event.
3192 + * We can get stuck here if buggy protocols don't correctly
3193 + * call dev_put. 
3194 + */
3195 +static void netdev_wait_allrefs(struct net_device *dev)
3196 +{
3197 +       unsigned long rebroadcast_time, warning_time;
3198 +
3199 +       rebroadcast_time = warning_time = jiffies;
3200 +       while (atomic_read(&dev->refcnt) != 0) {
3201 +               if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
3202 +                       rtnl_shlock();
3203 +
3204 +                       /* Rebroadcast unregister notification */
3205 +                       notifier_call_chain(&netdev_chain,
3206 +                                           NETDEV_UNREGISTER, dev);
3207 +
3208 +                       if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3209 +                                    &dev->state)) {
3210 +                               /* We must not have linkwatch events
3211 +                                * pending on unregister. If this
3212 +                                * happens, we simply run the queue
3213 +                                * unscheduled, resulting in a noop
3214 +                                * for this device.
3215 +                                */
3216 +                               linkwatch_run_queue();
3217 +                       }
3218 +
3219 +                       rtnl_shunlock();
3220 +
3221 +                       rebroadcast_time = jiffies;
3222 +               }
3223 +
3224 +               msleep(250);
3225 +
3226 +               if (time_after(jiffies, warning_time + 10 * HZ)) {
3227 +                       printk(KERN_EMERG "unregister_netdevice: "
3228 +                              "waiting for %s to become free. Usage "
3229 +                              "count = %d\n",
3230 +                              dev->name, atomic_read(&dev->refcnt));
3231 +                       warning_time = jiffies;
3232 +               }
3233 +       }
3234 +}
3235 +
3236 +/* The sequence is:
3237 + *
3238 + *     rtnl_lock();
3239 + *     ...
3240 + *     register_netdevice(x1);
3241 + *     register_netdevice(x2);
3242 + *     ...
3243 + *     unregister_netdevice(y1);
3244 + *     unregister_netdevice(y2);
3245 + *      ...
3246 + *     rtnl_unlock();
3247 + *     free_netdev(y1);
3248 + *     free_netdev(y2);
3249 + *
3250 + * We are invoked by rtnl_unlock() after it drops the semaphore.
3251 + * This allows us to deal with problems:
3252 + * 1) We can create/delete sysfs objects which invoke hotplug
3253 + *    without deadlocking with linkwatch via keventd.
3254 + * 2) Since we run with the RTNL semaphore not held, we can sleep
3255 + *    safely in order to wait for the netdev refcnt to drop to zero.
3256 + */
3257 +static DECLARE_MUTEX(net_todo_run_mutex);
3258 +void netdev_run_todo(void)
3259 +{
3260 +       struct list_head list = LIST_HEAD_INIT(list);
3261 +       int err;
3262 +
3263 +
3264 +       /* Need to guard against multiple cpu's getting out of order. */
3265 +       down(&net_todo_run_mutex);
3266 +
3267 +       /* Not safe to do outside the semaphore.  We must not return
3268 +        * until all unregister events invoked by the local processor
3269 +        * have been completed (either by this todo run, or one on
3270 +        * another cpu).
3271 +        */
3272 +       if (list_empty(&net_todo_list))
3273 +               goto out;
3274 +
3275 +       /* Snapshot list, allow later requests */
3276 +       spin_lock(&net_todo_list_lock);
3277 +       list_splice_init(&net_todo_list, &list);
3278 +       spin_unlock(&net_todo_list_lock);
3279 +               
3280 +       while (!list_empty(&list)) {
3281 +               struct net_device *dev
3282 +                       = list_entry(list.next, struct net_device, todo_list);
3283 +               list_del(&dev->todo_list);
3284 +
3285 +               switch(dev->reg_state) {
3286 +               case NETREG_REGISTERING:
3287 +                       err = netdev_register_sysfs(dev);
3288 +                       if (err)
3289 +                               printk(KERN_ERR "%s: failed sysfs registration (%d)\n",
3290 +                                      dev->name, err);
3291 +                       dev->reg_state = NETREG_REGISTERED;
3292 +                       break;
3293 +
3294 +               case NETREG_UNREGISTERING:
3295 +                       netdev_unregister_sysfs(dev);
3296 +                       dev->reg_state = NETREG_UNREGISTERED;
3297 +
3298 +                       netdev_wait_allrefs(dev);
3299 +
3300 +                       /* paranoia */
3301 +                       BUG_ON(atomic_read(&dev->refcnt));
3302 +                       BUG_TRAP(!dev->ip_ptr);
3303 +                       BUG_TRAP(!dev->ip6_ptr);
3304 +                       BUG_TRAP(!dev->dn_ptr);
3305 +
3306 +
3307 +                       /* It must be the very last action, 
3308 +                        * after this 'dev' may point to freed up memory.
3309 +                        */
3310 +                       if (dev->destructor)
3311 +                               dev->destructor(dev);
3312 +                       break;
3313 +
3314 +               default:
3315 +                       printk(KERN_ERR "network todo '%s' but state %d\n",
3316 +                              dev->name, dev->reg_state);
3317 +                       break;
3318 +               }
3319 +       }
3320 +
3321 +out:
3322 +       up(&net_todo_run_mutex);
3323 +}
3324 +
3325 +/**
3326 + *     alloc_netdev - allocate network device
3327 + *     @sizeof_priv:   size of private data to allocate space for
3328 + *     @name:          device name format string
3329 + *     @setup:         callback to initialize device
3330 + *
3331 + *     Allocates a struct net_device with private data area for driver use
3332 + *     and performs basic initialization.
3333 + */
3334 +struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3335 +               void (*setup)(struct net_device *))
3336 +{
3337 +       void *p;
3338 +       struct net_device *dev;
3339 +       int alloc_size;
3340 +
3341 +       /* ensure 32-byte alignment of both the device and private area */
3342 +       alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3343 +       alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3344 +
3345 +       p = kmalloc(alloc_size, GFP_KERNEL);
3346 +       if (!p) {
3347 +               printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
3348 +               return NULL;
3349 +       }
3350 +       memset(p, 0, alloc_size);
3351 +
3352 +       dev = (struct net_device *)
3353 +               (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3354 +       dev->padded = (char *)dev - (char *)p;
3355 +
3356 +       if (sizeof_priv)
3357 +               dev->priv = netdev_priv(dev);
3358 +
3359 +       setup(dev);
3360 +       strcpy(dev->name, name);
3361 +       return dev;
3362 +}
3363 +EXPORT_SYMBOL(alloc_netdev);
3364 +
3365 +/**
3366 + *     free_netdev - free network device
3367 + *     @dev: device
3368 + *
3369 + *     This function does the last stage of destroying an allocated device 
3370 + *     interface. The reference to the device object is released.  
3371 + *     If this is the last reference then it will be freed.
3372 + */
3373 +void free_netdev(struct net_device *dev)
3374 +{
3375 +#ifdef CONFIG_SYSFS
3376 +       /*  Compatiablity with error handling in drivers */
3377 +       if (dev->reg_state == NETREG_UNINITIALIZED) {
3378 +               kfree((char *)dev - dev->padded);
3379 +               return;
3380 +       }
3381 +
3382 +       BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3383 +       dev->reg_state = NETREG_RELEASED;
3384 +
3385 +       /* will free via class release */
3386 +       class_device_put(&dev->class_dev);
3387 +#else
3388 +       kfree((char *)dev - dev->padded);
3389 +#endif
3390 +}
3391
3392 +/* Synchronize with packet receive processing. */
3393 +void synchronize_net(void) 
3394 +{
3395 +       might_sleep();
3396 +       synchronize_rcu();
3397 +}
3398 +
3399 +/**
3400 + *     unregister_netdevice - remove device from the kernel
3401 + *     @dev: device
3402 + *
3403 + *     This function shuts down a device interface and removes it
3404 + *     from the kernel tables. On success 0 is returned, on a failure
3405 + *     a negative errno code is returned.
3406 + *
3407 + *     Callers must hold the rtnl semaphore.  You may want
3408 + *     unregister_netdev() instead of this.
3409 + */
3410 +
3411 +int unregister_netdevice(struct net_device *dev)
3412 +{
3413 +       struct net_device *d, **dp;
3414 +
3415 +       BUG_ON(dev_boot_phase);
3416 +       ASSERT_RTNL();
3417 +
3418 +       /* Some devices call without registering for initialization unwind. */
3419 +       if (dev->reg_state == NETREG_UNINITIALIZED) {
3420 +               printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3421 +                                 "was registered\n", dev->name, dev);
3422 +               return -ENODEV;
3423 +       }
3424 +
3425 +       BUG_ON(dev->reg_state != NETREG_REGISTERED);
3426 +
3427 +       /* If device is running, close it first. */
3428 +       if (dev->flags & IFF_UP)
3429 +               dev_close(dev);
3430 +
3431 +       /* And unlink it from device chain. */
3432 +       for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
3433 +               if (d == dev) {
3434 +                       write_lock_bh(&dev_base_lock);
3435 +                       hlist_del(&dev->name_hlist);
3436 +                       hlist_del(&dev->index_hlist);
3437 +                       if (dev_tail == &dev->next)
3438 +                               dev_tail = dp;
3439 +                       *dp = d->next;
3440 +                       write_unlock_bh(&dev_base_lock);
3441 +                       break;
3442 +               }
3443 +       }
3444 +       if (!d) {
3445 +               printk(KERN_ERR "unregister net_device: '%s' not found\n",
3446 +                      dev->name);
3447 +               return -ENODEV;
3448 +       }
3449 +
3450 +       dev->reg_state = NETREG_UNREGISTERING;
3451 +
3452 +       synchronize_net();
3453 +
3454 +       /* Shutdown queueing discipline. */
3455 +       dev_shutdown(dev);
3456 +
3457 +       
3458 +       /* Notify protocols, that we are about to destroy
3459 +          this device. They should clean all the things.
3460 +       */
3461 +       notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3462 +       
3463 +       /*
3464 +        *      Flush the multicast chain
3465 +        */
3466 +       dev_mc_discard(dev);
3467 +
3468 +       if (dev->uninit)
3469 +               dev->uninit(dev);
3470 +
3471 +       /* Notifier chain MUST detach us from master device. */
3472 +       BUG_TRAP(!dev->master);
3473 +
3474 +       free_divert_blk(dev);
3475 +
3476 +       /* Finish processing unregister after unlock */
3477 +       net_set_todo(dev);
3478 +
3479 +       synchronize_net();
3480 +
3481 +       dev_put(dev);
3482 +       return 0;
3483 +}
3484 +
3485 +/**
3486 + *     unregister_netdev - remove device from the kernel
3487 + *     @dev: device
3488 + *
3489 + *     This function shuts down a device interface and removes it
3490 + *     from the kernel tables. On success 0 is returned, on a failure
3491 + *     a negative errno code is returned.
3492 + *
3493 + *     This is just a wrapper for unregister_netdevice that takes
3494 + *     the rtnl semaphore.  In general you want to use this and not
3495 + *     unregister_netdevice.
3496 + */
3497 +void unregister_netdev(struct net_device *dev)
3498 +{
3499 +       rtnl_lock();
3500 +       unregister_netdevice(dev);
3501 +       rtnl_unlock();
3502 +}
3503 +
3504 +EXPORT_SYMBOL(unregister_netdev);
3505 +
3506 +#ifdef CONFIG_HOTPLUG_CPU
3507 +static int dev_cpu_callback(struct notifier_block *nfb,
3508 +                           unsigned long action,
3509 +                           void *ocpu)
3510 +{
3511 +       struct sk_buff **list_skb;
3512 +       struct net_device **list_net;
3513 +       struct sk_buff *skb;
3514 +       unsigned int cpu, oldcpu = (unsigned long)ocpu;
3515 +       struct softnet_data *sd, *oldsd;
3516 +
3517 +       if (action != CPU_DEAD)
3518 +               return NOTIFY_OK;
3519 +
3520 +       local_irq_disable();
3521 +       cpu = smp_processor_id();
3522 +       sd = &per_cpu(softnet_data, cpu);
3523 +       oldsd = &per_cpu(softnet_data, oldcpu);
3524 +
3525 +       /* Find end of our completion_queue. */
3526 +       list_skb = &sd->completion_queue;
3527 +       while (*list_skb)
3528 +               list_skb = &(*list_skb)->next;
3529 +       /* Append completion queue from offline CPU. */
3530 +       *list_skb = oldsd->completion_queue;
3531 +       oldsd->completion_queue = NULL;
3532 +
3533 +       /* Find end of our output_queue. */
3534 +       list_net = &sd->output_queue;
3535 +       while (*list_net)
3536 +               list_net = &(*list_net)->next_sched;
3537 +       /* Append output queue from offline CPU. */
3538 +       *list_net = oldsd->output_queue;
3539 +       oldsd->output_queue = NULL;
3540 +
3541 +       raise_softirq_irqoff(NET_TX_SOFTIRQ);
3542 +       local_irq_enable();
3543 +
3544 +       /* Process offline CPU's input_pkt_queue */
3545 +       while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3546 +               netif_rx(skb);
3547 +
3548 +       return NOTIFY_OK;
3549 +}
3550 +#endif /* CONFIG_HOTPLUG_CPU */
3551 +
3552 +
3553 +/*
3554 + *     Initialize the DEV module. At boot time this walks the device list and
3555 + *     unhooks any devices that fail to initialise (normally hardware not
3556 + *     present) and leaves us with a valid list of present and active devices.
3557 + *
3558 + */
3559 +
3560 +/*
3561 + *       This is called single threaded during boot, so no need
3562 + *       to take the rtnl semaphore.
3563 + */
3564 +static int __init net_dev_init(void)
3565 +{
3566 +       int i, rc = -ENOMEM;
3567 +
3568 +       BUG_ON(!dev_boot_phase);
3569 +
3570 +       net_random_init();
3571 +
3572 +       if (dev_proc_init())
3573 +               goto out;
3574 +
3575 +       if (netdev_sysfs_init())
3576 +               goto out;
3577 +
3578 +       INIT_LIST_HEAD(&ptype_all);
3579 +       for (i = 0; i < 16; i++) 
3580 +               INIT_LIST_HEAD(&ptype_base[i]);
3581 +
3582 +       for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3583 +               INIT_HLIST_HEAD(&dev_name_head[i]);
3584 +
3585 +       for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3586 +               INIT_HLIST_HEAD(&dev_index_head[i]);
3587 +
3588 +       /*
3589 +        *      Initialise the packet receive queues.
3590 +        */
3591 +
3592 +       for (i = 0; i < NR_CPUS; i++) {
3593 +               struct softnet_data *queue;
3594 +
3595 +               queue = &per_cpu(softnet_data, i);
3596 +               skb_queue_head_init(&queue->input_pkt_queue);
3597 +               queue->throttle = 0;
3598 +               queue->cng_level = 0;
3599 +               queue->avg_blog = 10; /* arbitrary non-zero */
3600 +               queue->completion_queue = NULL;
3601 +               INIT_LIST_HEAD(&queue->poll_list);
3602 +               set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3603 +               queue->backlog_dev.weight = weight_p;
3604 +               queue->backlog_dev.poll = process_backlog;
3605 +               atomic_set(&queue->backlog_dev.refcnt, 1);
3606 +       }
3607 +
3608 +#ifdef OFFLINE_SAMPLE
3609 +       samp_timer.expires = jiffies + (10 * HZ);
3610 +       add_timer(&samp_timer);
3611 +#endif
3612 +
3613 +       dev_boot_phase = 0;
3614 +
3615 +       open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3616 +       open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3617 +
3618 +       hotcpu_notifier(dev_cpu_callback, 0);
3619 +       dst_init();
3620 +       dev_mcast_init();
3621 +       rc = 0;
3622 +out:
3623 +       return rc;
3624 +}
3625 +
3626 +subsys_initcall(net_dev_init);
3627 +
3628 +EXPORT_SYMBOL(__dev_get_by_index);
3629 +EXPORT_SYMBOL(__dev_get_by_name);
3630 +EXPORT_SYMBOL(__dev_remove_pack);
3631 +EXPORT_SYMBOL(__skb_linearize);
3632 +EXPORT_SYMBOL(dev_add_pack);
3633 +EXPORT_SYMBOL(dev_alloc_name);
3634 +EXPORT_SYMBOL(dev_close);
3635 +EXPORT_SYMBOL(dev_get_by_flags);
3636 +EXPORT_SYMBOL(dev_get_by_index);
3637 +EXPORT_SYMBOL(dev_get_by_name);
3638 +EXPORT_SYMBOL(dev_ioctl);
3639 +EXPORT_SYMBOL(dev_open);
3640 +EXPORT_SYMBOL(dev_queue_xmit);
3641 +EXPORT_SYMBOL(dev_remove_pack);
3642 +EXPORT_SYMBOL(dev_set_allmulti);
3643 +EXPORT_SYMBOL(dev_set_promiscuity);
3644 +EXPORT_SYMBOL(dev_change_flags);
3645 +EXPORT_SYMBOL(dev_set_mtu);
3646 +EXPORT_SYMBOL(dev_set_mac_address);
3647 +EXPORT_SYMBOL(free_netdev);
3648 +EXPORT_SYMBOL(netdev_boot_setup_check);
3649 +EXPORT_SYMBOL(netdev_set_master);
3650 +EXPORT_SYMBOL(netdev_state_change);
3651 +EXPORT_SYMBOL(netif_receive_skb);
3652 +EXPORT_SYMBOL(netif_rx);
3653 +EXPORT_SYMBOL(register_gifconf);
3654 +EXPORT_SYMBOL(register_netdevice);
3655 +EXPORT_SYMBOL(register_netdevice_notifier);
3656 +EXPORT_SYMBOL(skb_checksum_help);
3657 +EXPORT_SYMBOL(synchronize_net);
3658 +EXPORT_SYMBOL(unregister_netdevice);
3659 +EXPORT_SYMBOL(unregister_netdevice_notifier);
3660 +EXPORT_SYMBOL(net_enable_timestamp);
3661 +EXPORT_SYMBOL(net_disable_timestamp);
3662 +EXPORT_SYMBOL(dev_get_flags);
3663 +
3664 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3665 +EXPORT_SYMBOL(br_handle_frame_hook);
3666 +EXPORT_SYMBOL(br_fdb_get_hook);
3667 +EXPORT_SYMBOL(br_fdb_put_hook);
3668 +#endif
3669 +
3670 +#ifdef CONFIG_KMOD
3671 +EXPORT_SYMBOL(dev_load);
3672 +#endif
3673 +
3674 +EXPORT_PER_CPU_SYMBOL(softnet_data);
3675 diff --unified --recursive --new-file linux-2.6.12.5/net/ring/Kconfig linux-2.6.12.5-1-686-smp-ring3/net/ring/Kconfig
3676 --- linux-2.6.12.5/net/ring/Kconfig     1970-01-01 01:00:00.000000000 +0100
3677 +++ linux-2.6.12.5-1-686-smp-ring3/net/ring/Kconfig     2005-10-22 23:50:45.539482000 +0200
3678 @@ -0,0 +1,14 @@
3679 +config RING
3680 +       tristate "PF_RING sockets (EXPERIMENTAL)"
3681 +       depends on EXPERIMENTAL
3682 +       ---help---
3683 +         PF_RING socket family, optimized for packet capture.
3684 +          If a PF_RING socket is bound to an adapter (via the bind() system
3685 +          call), such adapter will be used in read-only mode until the socket
3686 +          is destroyed. Whenever an incoming packet is received from the adapter
3687 +          it will not passed to upper layers, but instead it is copied to a ring
3688 +          buffer, which in turn is exported to user space applications via mmap.
3689 +          Please refer to http://luca.ntop.org/Ring.pdf for more.
3690 +
3691 +         Say N unless you know what you are doing.
3692 +
3693 diff --unified --recursive --new-file linux-2.6.12.5/net/ring/Makefile linux-2.6.12.5-1-686-smp-ring3/net/ring/Makefile
3694 --- linux-2.6.12.5/net/ring/Makefile    1970-01-01 01:00:00.000000000 +0100
3695 +++ linux-2.6.12.5-1-686-smp-ring3/net/ring/Makefile    2005-10-22 23:50:45.051451500 +0200
3696 @@ -0,0 +1,7 @@
3697 +#
3698 +# Makefile for the ring driver.
3699 +#
3700 +
3701 +obj-m += ring.o
3702 +
3703 +ring-objs := ring_packet.o
3704 diff --unified --recursive --new-file linux-2.6.12.5/net/ring/ring_packet.c linux-2.6.12.5-1-686-smp-ring3/net/ring/ring_packet.c
3705 --- linux-2.6.12.5/net/ring/ring_packet.c       1970-01-01 01:00:00.000000000 +0100
3706 +++ linux-2.6.12.5-1-686-smp-ring3/net/ring/ring_packet.c       2005-10-22 23:50:45.159458250 +0200
3707 @@ -0,0 +1,1592 @@
3708 +/*
3709 + *
3710 + * (C) 2004-05 - Luca Deri <deri@ntop.org>
3711 + *
3712 + * This code includes patches courtesy of
3713 + * - Jeff Randall <jrandall@nexvu.com>
3714 + * - Helmut Manck <helmut.manck@secunet.com>
3715 + * - Brad Doctor <bdoctor@ps-ax.com>
3716 + *
3717 + */
3718 +
3719 +/* FIX: add an entry inside the /proc filesystem */
3720 +
3721 +#include <linux/version.h>
3722 +#include <linux/config.h>
3723 +#include <linux/module.h>
3724 +#include <linux/kernel.h>
3725 +#include <linux/socket.h>
3726 +#include <linux/skbuff.h>
3727 +#include <linux/rtnetlink.h>
3728 +#include <linux/in.h>
3729 +#include <linux/in6.h>
3730 +#include <linux/init.h>
3731 +#include <linux/filter.h>
3732 +#include <linux/ring.h>
3733 +#include <linux/ip.h>
3734 +#include <linux/tcp.h>
3735 +#include <linux/udp.h>
3736 +#include <linux/list.h>
3737 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
3738 +#include <net/xfrm.h>
3739 +#else
3740 +#include <linux/poll.h>
3741 +#endif
3742 +#include <net/sock.h>
3743 +#include <asm/io.h>   /* needed for virt_to_phys() */
3744 +
3745 +/* #define RING_DEBUG */
3746 +
3747 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11))
3748 +static inline int remap_page_range(struct vm_area_struct *vma,
3749 +                                  unsigned long uvaddr,
3750 +                                  unsigned long paddr,
3751 +                                  unsigned long size,
3752 +                                  pgprot_t prot) {
3753 +  return(remap_pfn_range(vma, uvaddr, paddr >> PAGE_SHIFT,
3754 +                        size, prot));
3755 +}
3756 +#endif
3757 +
3758 +/* ************************************************* */
3759 +
3760 +#define CLUSTER_LEN       8
3761 +
3762 +struct ring_cluster {
3763 +  u_short             cluster_id; /* 0 = no cluster */
3764 +  u_short             num_cluster_elements;
3765 +  enum cluster_type   hashing_mode;
3766 +  u_short             hashing_id;
3767 +  struct sock         *sk[CLUSTER_LEN];
3768 +  struct ring_cluster *next;      /* NULL = last element of the cluster */
3769 +};
3770 +
3771 +/* ************************************************* */
3772 +
3773 +struct ring_element {
3774 +  struct list_head  list;
3775 +  struct sock      *sk;
3776 +};
3777 +
3778 +/* ************************************************* */
3779 +
3780 +struct ring_opt {
3781 +  struct net_device *ring_netdev;
3782 +
3783 +  /* Cluster */
3784 +  u_short cluster_id; /* 0 = no cluster */
3785 +
3786 +  /* Reflector */
3787 +  struct net_device *reflector_dev;
3788 +
3789 +  /* Packet buffers */
3790 +  unsigned long order;
3791 +
3792 +  /* Ring Slots */
3793 +  unsigned long ring_memory;
3794 +  FlowSlotInfo *slots_info; /* Basically it points to ring_memory */
3795 +  char *ring_slots;  /* Basically it points to ring_memory
3796 +                       +sizeof(FlowSlotInfo) */
3797 +
3798 +  /* Packet Sampling */
3799 +  u_int pktToSample, sample_rate;
3800 +
3801 +  /* BPF Filter */
3802 +  struct sk_filter *bpfFilter;
3803 +
3804 +  /* Locks */
3805 +  atomic_t num_ring_slots_waiters;
3806 +  wait_queue_head_t ring_slots_waitqueue;
3807 +  rwlock_t ring_index_lock;
3808 +
3809 +  /* Indexes (Internal) */
3810 +  u_int insert_page_id, insert_slot_id;
3811 +};
3812 +
3813 +/* ************************************************* */
3814 +
3815 +/* List of all ring sockets. */
3816 +static struct list_head ring_table;
3817 +
3818 +/* List of all clusters */
3819 +static struct ring_cluster *ring_cluster_list;
3820 +
3821 +static rwlock_t ring_mgmt_lock = RW_LOCK_UNLOCKED;
3822 +
3823 +/* ********************************** */
3824 +
3825 +/* Forward */
3826 +static struct proto_ops ring_ops;
3827 +
3828 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
3829 +static struct proto ring_proto;
3830 +#endif
3831 +
3832 +static int skb_ring_handler(struct sk_buff *skb, u_char recv_packet,
3833 +                           u_char real_skb);
3834 +static int buffer_ring_handler(struct net_device *dev, char *data, int len);
3835 +static int remove_from_cluster(struct sock *sock, struct ring_opt *pfr);
3836 +
3837 +/* Extern */
3838 +
3839 +/* ********************************** */
3840 +
3841 +/* Defaults */
3842 +static u_int bucket_len = 128, num_slots = 4096, sample_rate = 1,
3843 +  transparent_mode = 0, enable_tx_capture = 0;
3844 +
3845 +MODULE_PARM(bucket_len, "i");
3846 +MODULE_PARM_DESC(bucket_len, "Number of ring buckets");
3847 +MODULE_PARM(num_slots,  "i");
3848 +MODULE_PARM_DESC(num_slots,  "Number of ring slots");
3849 +MODULE_PARM(sample_rate, "i");
3850 +MODULE_PARM_DESC(sample_rate, "Ring packet sample rate");
3851 +MODULE_PARM(transparent_mode, "i");
3852 +MODULE_PARM_DESC(transparent_mode,
3853 +                "Set to 1 to set transparent mode "
3854 +                "(slower but backwards compatible)");
3855 +MODULE_PARM(enable_tx_capture, "i");
3856 +MODULE_PARM_DESC(enable_tx_capture, "Set to 1 to capture outgoing packets");
3857 +
3858 +/* ********************************** */
3859 +
3860 +#define MIN_QUEUED_PKTS      64
3861 +#define MAX_QUEUE_LOOPS      64
3862 +
3863 +
3864 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
3865 +#define ring_sk_datatype(__sk) ((struct ring_opt *)__sk)
3866 +#define ring_sk(__sk) ((__sk)->sk_protinfo)
3867 +#else
3868 +#define ring_sk_datatype(a) (a)
3869 +#define ring_sk(__sk) ((__sk)->protinfo.pf_ring)
3870 +#endif
3871 +
3872 +/*
3873 +  int dev_queue_xmit(struct sk_buff *skb)
3874 +  skb->dev;
3875 +  struct net_device *dev_get_by_name(const char *name)
3876 +*/
3877 +
3878 +/* ********************************** */
3879 +
3880 +static void ring_sock_destruct(struct sock *sk) {
3881 +
3882 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
3883 +  skb_queue_purge(&sk->sk_receive_queue);
3884 +
3885 +  if (!sock_flag(sk, SOCK_DEAD)) {
3886 +#if defined(RING_DEBUG)
3887 +    printk("Attempt to release alive ring socket: %p\n", sk);
3888 +#endif
3889 +    return;
3890 +  }
3891 +
3892 +  BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
3893 +  BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
3894 +#else
3895 +
3896 +  BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
3897 +  BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
3898 +
3899 +  if (!sk->dead) {
3900 +#if defined(RING_DEBUG)
3901 +    printk("Attempt to release alive ring socket: %p\n", sk);
3902 +#endif
3903 +    return;
3904 +  }
3905 +#endif
3906 +
3907 +  kfree(ring_sk(sk));
3908 +
3909 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
3910 +  MOD_DEC_USE_COUNT;
3911 +#endif
3912 +}
3913 +
3914 +/* ********************************** */
3915 +/*
3916 + * ring_insert()
3917 + *
3918 + * store the sk in a new element and add it
3919 + * to the head of the list.
3920 + */
3921 +static inline void ring_insert(struct sock *sk) {
3922 +  struct ring_element *next;
3923 +
3924 +#if defined(RING_DEBUG)
3925 +  printk("RING: ring_insert()\n");
3926 +#endif
3927 +
3928 +  next = kmalloc(sizeof(struct ring_element), GFP_ATOMIC);
3929 +  if(next != NULL) {
3930 +    next->sk = sk;
3931 +    write_lock_irq(&ring_mgmt_lock);
3932 +    list_add(&next->list, &ring_table);
3933 +    write_unlock_irq(&ring_mgmt_lock);
3934 +  } else {
3935 +    if (net_ratelimit())
3936 +      printk("RING: could not kmalloc slot!!\n");
3937 +  }
3938 +}
3939 +
3940 +/* ********************************** */
3941 +/*
3942 + * ring_remove()
3943 + *
3944 + * For each of the elements in the list:
3945 + *  - check if this is the element we want to delete
3946 + *  - if it is, remove it from the list, and free it.
3947 + *
3948 + * stop when we find the one we're looking for (break),
3949 + * or when we reach the end of the list.
3950 + */
3951 +static inline void ring_remove(struct sock *sk) {
3952 +  struct list_head *ptr;
3953 +  struct ring_element *entry;
3954 +
3955 +
3956 +  for(ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
3957 +    entry = list_entry(ptr, struct ring_element, list);
3958 +
3959 +    if(entry->sk == sk) {
3960 +      write_lock_irq(&ring_mgmt_lock);
3961 +      list_del(ptr);
3962 +      kfree(ptr);
3963 +      write_unlock_irq(&ring_mgmt_lock);
3964 +      break;
3965 +    }
3966 +  }
3967 +
3968 +}
3969 +
3970 +/* ********************************** */
3971 +
3972 +static u_int32_t num_queued_pkts(struct ring_opt *pfr) {
3973 +
3974 +  if(pfr->ring_slots != NULL) {
3975 +
3976 +    u_int32_t tot_insert = pfr->slots_info->insert_idx,
3977 +#if defined(RING_DEBUG)
3978 +      tot_read = pfr->slots_info->tot_read, tot_pkts;
3979 +#else
3980 +    tot_read = pfr->slots_info->tot_read;
3981 +#endif
3982 +
3983 +    if(tot_insert >= tot_read) {
3984 +#if defined(RING_DEBUG)
3985 +      tot_pkts = tot_insert-tot_read;
3986 +#endif
3987 +      return(tot_insert-tot_read);
3988 +    } else {
3989 +#if defined(RING_DEBUG)
3990 +      tot_pkts = ((u_int32_t)-1)+tot_insert-tot_read;
3991 +#endif
3992 +      return(((u_int32_t)-1)+tot_insert-tot_read);
3993 +    }
3994 +
3995 +#if defined(RING_DEBUG)
3996 +    printk("-> num_queued_pkts=%d [tot_insert=%d][tot_read=%d]\n",
3997 +          tot_pkts, tot_insert, tot_read);
3998 +#endif
3999 +
4000 +  } else
4001 +    return(0);
4002 +}
4003 +
4004 +/* ********************************** */
4005 +
4006 +static inline FlowSlot* get_insert_slot(struct ring_opt *pfr) {
4007 +#if defined(RING_DEBUG)
4008 +  printk("get_insert_slot(%d)\n", pfr->slots_info->insert_idx);
4009 +#endif
4010 +
4011 +  if(pfr->ring_slots != NULL) {
4012 +    FlowSlot *slot = (FlowSlot*)&(pfr->ring_slots[pfr->slots_info->insert_idx
4013 +                                                 *pfr->slots_info->slot_len]);
4014 +    return(slot);
4015 +  } else
4016 +    return(NULL);
4017 +}
4018 +
4019 +/* ********************************** */
4020 +
4021 +static inline FlowSlot* get_remove_slot(struct ring_opt *pfr) {
4022 +#if defined(RING_DEBUG)
4023 +  printk("get_remove_slot(%d)\n", pfr->slots_info->remove_idx);
4024 +#endif
4025 +
4026 +  if(pfr->ring_slots != NULL)
4027 +    return((FlowSlot*)&(pfr->ring_slots[pfr->slots_info->remove_idx*
4028 +                                       pfr->slots_info->slot_len]));
4029 +  else
4030 +    return(NULL);
4031 +}
4032 +
4033 +/* ********************************** */
4034 +
4035 +static void add_skb_to_ring(struct sk_buff *skb,
4036 +                           struct ring_opt *pfr,
4037 +                           u_char recv_packet,
4038 +                           u_char real_skb /* 1=skb 0=faked skb */) {
4039 +  FlowSlot *theSlot;
4040 +  int idx, displ;
4041 +
4042 +  if(recv_packet) {
4043 +    /* Hack for identifying a packet received by the e1000 */
4044 +    if(real_skb) {
4045 +      displ = SKB_DISPLACEMENT;
4046 +    } else
4047 +      displ = 0; /* Received by the e1000 wrapper */
4048 +  } else
4049 +    displ = 0;
4050 +
4051 +  write_lock(&pfr->ring_index_lock);
4052 +  pfr->slots_info->tot_pkts++;
4053 +  write_unlock(&pfr->ring_index_lock);
4054 +
4055 +  /* BPF Filtering (from af_packet.c) */
4056 +  if(pfr->bpfFilter != NULL) {
4057 +    unsigned res = 1, len;
4058 +
4059 +    len = skb->len-skb->data_len;
4060 +
4061 +    write_lock(&pfr->ring_index_lock);
4062 +    skb->data -= displ;
4063 +    res = sk_run_filter(skb, pfr->bpfFilter->insns, pfr->bpfFilter->len);
4064 +    skb->data += displ;
4065 +    write_unlock(&pfr->ring_index_lock);
4066 +
4067 +    if(res == 0) {
4068 +      /* Filter failed */
4069 +
4070 +#if defined(RING_DEBUG)
4071 +      printk("add_skb_to_ring(skb): Filter failed [len=%d][tot=%llu]"
4072 +            "[insertIdx=%d][pkt_type=%d][cloned=%d]\n",
4073 +            (int)skb->len, pfr->slots_info->tot_pkts,
4074 +            pfr->slots_info->insert_idx,
4075 +            skb->pkt_type, skb->cloned);
4076 +#endif
4077 +
4078 +      return;
4079 +    }
4080 +  }
4081 +
4082 +  /* ************************** */
4083 +
4084 +  if(pfr->sample_rate > 1) {
4085 +    if(pfr->pktToSample == 0) {
4086 +      write_lock(&pfr->ring_index_lock);
4087 +      pfr->pktToSample = pfr->sample_rate;
4088 +      write_unlock(&pfr->ring_index_lock);
4089 +    } else {
4090 +      write_lock(&pfr->ring_index_lock);
4091 +      pfr->pktToSample--;
4092 +      write_unlock(&pfr->ring_index_lock);
4093 +
4094 +#if defined(RING_DEBUG)
4095 +      printk("add_skb_to_ring(skb): sampled packet [len=%d]"
4096 +            "[tot=%llu][insertIdx=%d][pkt_type=%d][cloned=%d]\n",
4097 +            (int)skb->len, pfr->slots_info->tot_pkts,
4098 +            pfr->slots_info->insert_idx,
4099 +            skb->pkt_type, skb->cloned);
4100 +#endif
4101 +      return;
4102 +    }
4103 +  }
4104 +
4105 +  /* ************************************* */
4106 +
4107 +  if((pfr->reflector_dev != NULL)
4108 +     && (!netif_queue_stopped(pfr->reflector_dev))) {
4109 +    int cpu = smp_processor_id();
4110 +
4111 +    /* increase reference counter so that this skb is not freed */
4112 +    atomic_inc(&skb->users);
4113 +
4114 +    skb->data -= displ;
4115 +
4116 +    /* send it */
4117 +    if (pfr->reflector_dev->xmit_lock_owner != cpu) {
4118 +      spin_lock_bh(&pfr->reflector_dev->xmit_lock);
4119 +      pfr->reflector_dev->xmit_lock_owner = cpu;
4120 +      spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
4121 +
4122 +      if (pfr->reflector_dev->hard_start_xmit(skb,
4123 +                                             pfr->reflector_dev) == 0) {
4124 +        spin_lock_bh(&pfr->reflector_dev->xmit_lock);
4125 +       pfr->reflector_dev->xmit_lock_owner = -1;
4126 +       skb->data += displ;
4127 +       spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
4128 +#if defined(RING_DEBUG)
4129 +       printk("++ hard_start_xmit succeeded\n");
4130 +#endif
4131 +       return; /* OK */
4132 +      }
4133 +
4134 +      spin_lock_bh(&pfr->reflector_dev->xmit_lock);
4135 +      pfr->reflector_dev->xmit_lock_owner = -1;
4136 +      spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
4137 +    }
4138 +
4139 +#if defined(RING_DEBUG)
4140 +    printk("++ hard_start_xmit failed\n");
4141 +#endif
4142 +    skb->data += displ;
4143 +    return; /* -ENETDOWN */
4144 +  }
4145 +
4146 +  /* ************************************* */
4147 +
4148 +#if defined(RING_DEBUG)
4149 +  printk("add_skb_to_ring(skb) [len=%d][tot=%llu][insertIdx=%d]"
4150 +        "[pkt_type=%d][cloned=%d]\n",
4151 +        (int)skb->len, pfr->slots_info->tot_pkts,
4152 +        pfr->slots_info->insert_idx,
4153 +        skb->pkt_type, skb->cloned);
4154 +#endif
4155 +
4156 +  idx = pfr->slots_info->insert_idx;
4157 +  theSlot = get_insert_slot(pfr);
4158 +
4159 +  if((theSlot != NULL) && (theSlot->slot_state == 0)) {
4160 +    struct pcap_pkthdr *hdr;
4161 +    unsigned int bucketSpace;
4162 +    char *bucket;
4163 +
4164 +    /* Update Index */
4165 +    idx++;
4166 +
4167 +    if(idx == pfr->slots_info->tot_slots) {
4168 +      write_lock(&pfr->ring_index_lock);
4169 +      pfr->slots_info->insert_idx = 0;
4170 +      write_unlock(&pfr->ring_index_lock);
4171 +    } else {
4172 +      write_lock(&pfr->ring_index_lock);
4173 +      pfr->slots_info->insert_idx = idx;
4174 +      write_unlock(&pfr->ring_index_lock);
4175 +    }
4176 +
4177 +    bucketSpace = pfr->slots_info->slot_len
4178 +#ifdef RING_MAGIC
4179 +      - sizeof(u_char)
4180 +#endif
4181 +      - sizeof(u_char)  /* flowSlot.slot_state */
4182 +      - sizeof(struct pcap_pkthdr)
4183 +      - 1 /* 10 */ /* safe boundary */;
4184 +
4185 +    bucket = &theSlot->bucket;
4186 +    hdr = (struct pcap_pkthdr*)bucket;
4187 +
4188 +    if(skb->stamp.tv_sec == 0) do_gettimeofday(&skb->stamp);
4189 +
4190 +    hdr->ts.tv_sec = skb->stamp.tv_sec, hdr->ts.tv_usec = skb->stamp.tv_usec;
4191 +    hdr->caplen    = skb->len+displ;
4192 +
4193 +    if(hdr->caplen > bucketSpace)
4194 +      hdr->caplen = bucketSpace;
4195 +
4196 +    hdr->len = skb->len+displ;
4197 +    memcpy(&bucket[sizeof(struct pcap_pkthdr)],
4198 +          skb->data-displ, hdr->caplen);
4199 +
4200 +#if defined(RING_DEBUG)
4201 +    {
4202 +      static unsigned int lastLoss = 0;
4203 +
4204 +      if(pfr->slots_info->tot_lost
4205 +        && (lastLoss != pfr->slots_info->tot_lost)) {
4206 +       printk("add_skb_to_ring(%d): [bucketSpace=%d]"
4207 +              "[hdr.caplen=%d][skb->len=%d]"
4208 +              "[pcap_pkthdr=%d][removeIdx=%d]"
4209 +              "[loss=%lu][page=%u][slot=%u]\n",
4210 +              idx-1, bucketSpace, hdr->caplen, skb->len,
4211 +              sizeof(struct pcap_pkthdr),
4212 +              pfr->slots_info->remove_idx,
4213 +              (long unsigned int)pfr->slots_info->tot_lost,
4214 +              pfr->insert_page_id, pfr->insert_slot_id);
4215 +
4216 +       lastLoss = pfr->slots_info->tot_lost;
4217 +      }
4218 +    }
4219 +#endif
4220 +
4221 +    write_lock(&pfr->ring_index_lock);
4222 +    pfr->slots_info->tot_insert++;
4223 +    theSlot->slot_state = 1;
4224 +    write_unlock(&pfr->ring_index_lock);
4225 +  } else {
4226 +    write_lock(&pfr->ring_index_lock);
4227 +    pfr->slots_info->tot_lost++;
4228 +    write_unlock(&pfr->ring_index_lock);
4229 +
4230 +#if defined(RING_DEBUG)
4231 +    printk("add_skb_to_ring(skb): packet lost [loss=%lu]"
4232 +          "[removeIdx=%u][insertIdx=%u]\n",
4233 +          (long unsigned int)pfr->slots_info->tot_lost,
4234 +          pfr->slots_info->remove_idx, pfr->slots_info->insert_idx);
4235 +#endif
4236 +  }
4237 +
4238 +  /* wakeup in case of poll() */
4239 +  if(waitqueue_active(&pfr->ring_slots_waitqueue))
4240 +    wake_up_interruptible(&pfr->ring_slots_waitqueue);
4241 +}
4242 +
4243 +/* ********************************** */
4244 +
4245 +static u_int hash_skb(struct ring_cluster *cluster_ptr,
4246 +                     struct sk_buff *skb, u_char recv_packet) {
4247 +  u_int idx;
4248 +  int displ;
4249 +  struct iphdr *ip;
4250 +
4251 +  if(cluster_ptr->hashing_mode == cluster_round_robin) {
4252 +    idx = cluster_ptr->hashing_id++;
4253 +  } else {
4254 +    /* Per-flow clustering */
4255 +    if(skb->len > sizeof(struct iphdr)+sizeof(struct tcphdr)) {
4256 +      if(recv_packet)
4257 +       displ = 0;
4258 +      else
4259 +       displ = SKB_DISPLACEMENT;
4260 +
4261 +      /*
4262 +       skb->data+displ
4263 +
4264 +       Always points to to the IP part of the packet
4265 +      */
4266 +
4267 +      ip = (struct iphdr*)(skb->data+displ);
4268 +
4269 +      idx = ip->saddr+ip->daddr+ip->protocol;
4270 +
4271 +      if(ip->protocol == IPPROTO_TCP) {
4272 +       struct tcphdr *tcp = (struct tcphdr*)(skb->data+displ
4273 +                                             +sizeof(struct iphdr));
4274 +       idx += tcp->source+tcp->dest;
4275 +      } else if(ip->protocol == IPPROTO_UDP) {
4276 +       struct udphdr *udp = (struct udphdr*)(skb->data+displ
4277 +                                             +sizeof(struct iphdr));
4278 +       idx += udp->source+udp->dest;
4279 +      }
4280 +    } else
4281 +      idx = skb->len;
4282 +  }
4283 +
4284 +  return(idx % cluster_ptr->num_cluster_elements);
4285 +}
4286 +
4287 +/* ********************************** */
4288 +
4289 +static int skb_ring_handler(struct sk_buff *skb,
4290 +                           u_char recv_packet,
4291 +                           u_char real_skb /* 1=skb 0=faked skb */) {
4292 +  struct sock *skElement;
4293 +  int rc = 0;
4294 +  struct list_head *ptr;
4295 +  struct ring_cluster *cluster_ptr;
4296 +
4297 +  if((!skb) /* Invalid skb */
4298 +     || ((!enable_tx_capture) && (!recv_packet))) {
4299 +    /*
4300 +      An outgoing packet is about to be sent out
4301 +      but we decided not to handle transmitted
4302 +      packets.
4303 +    */
4304 +    return(0);
4305 +  }
4306 +
4307 +#if defined(RING_DEBUG)
4308 +  if(0) {
4309 +    printk("skb_ring_handler() [len=%d][dev=%s]\n", skb->len,
4310 +          skb->dev->name == NULL ? "<NULL>" : skb->dev->name);
4311 +  }
4312 +#endif
4313 +
4314 +  /* [1] Check unclustered sockets */
4315 +  for (ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
4316 +    struct ring_opt *pfr;
4317 +    struct ring_element *entry;
4318 +
4319 +    entry = list_entry(ptr, struct ring_element, list);
4320 +
4321 +    read_lock(&ring_mgmt_lock);
4322 +    skElement = entry->sk;
4323 +    pfr = ring_sk(skElement);
4324 +    read_unlock(&ring_mgmt_lock);
4325 +
4326 +    if((pfr != NULL)
4327 +       && (pfr->cluster_id == 0 /* No cluster */)
4328 +       && (pfr->ring_slots != NULL)
4329 +       && (pfr->ring_netdev == skb->dev)) {
4330 +      /* We've found the ring where the packet can be stored */
4331 +      read_lock(&ring_mgmt_lock);
4332 +      add_skb_to_ring(skb, pfr, recv_packet, real_skb);
4333 +      read_unlock(&ring_mgmt_lock);
4334 +
4335 +      rc = 1; /* Ring found: we've done our job */
4336 +    }
4337 +  }
4338 +
4339 +  /* [2] Check socket clusters */
4340 +  cluster_ptr = ring_cluster_list;
4341 +
4342 +  while(cluster_ptr != NULL) {
4343 +    struct ring_opt *pfr;
4344 +
4345 +    if(cluster_ptr->num_cluster_elements > 0) {
4346 +      u_int skb_hash = hash_skb(cluster_ptr, skb, recv_packet);
4347 +
4348 +      read_lock(&ring_mgmt_lock);
4349 +      skElement = cluster_ptr->sk[skb_hash];
4350 +      read_unlock(&ring_mgmt_lock);
4351 +
4352 +      if(skElement != NULL) {
4353 +       pfr = ring_sk(skElement);
4354 +
4355 +       if((pfr != NULL)
4356 +          && (pfr->ring_slots != NULL)
4357 +          && (pfr->ring_netdev == skb->dev)) {
4358 +         /* We've found the ring where the packet can be stored */
4359 +          read_lock(&ring_mgmt_lock);
4360 +         add_skb_to_ring(skb, pfr, recv_packet, real_skb);
4361 +          read_unlock(&ring_mgmt_lock);
4362 +
4363 +         rc = 1; /* Ring found: we've done our job */
4364 +       }
4365 +      }
4366 +    }
4367 +
4368 +    cluster_ptr = cluster_ptr->next;
4369 +  }
4370 +
4371 +  if(transparent_mode) rc = 0;
4372 +
4373 +  if((rc != 0) && real_skb)
4374 +    dev_kfree_skb(skb); /* Free the skb */
4375 +
4376 +  return(rc); /*  0 = packet not handled */
4377 +}
4378 +
4379 +/* ********************************** */
4380 +
4381 +struct sk_buff skb;
4382 +
4383 +static int buffer_ring_handler(struct net_device *dev,
4384 +                              char *data, int len) {
4385 +
4386 +#if defined(RING_DEBUG)
4387 +  printk("buffer_ring_handler: [dev=%s][len=%d]\n",
4388 +        dev->name == NULL ? "<NULL>" : dev->name, len);
4389 +#endif
4390 +
4391 +  skb.dev = dev, skb.len = len, skb.data = data,
4392 +    skb.data_len = len, skb.stamp.tv_sec = 0; /* Calculate the time */
4393 +
4394 +  skb_ring_handler(&skb, 1, 0 /* fake skb */);
4395 +
4396 +  return(0);
4397 +}
4398 +
4399 +/* ********************************** */
4400 +
4401 +static int ring_create(struct socket *sock, int protocol) {
4402 +  struct sock *sk;
4403 +  struct ring_opt *pfr;
4404 +  int err;
4405 +
4406 +#if defined(RING_DEBUG)
4407 +  printk("RING: ring_create()\n");
4408 +#endif
4409 +
4410 +  /* Are you root, superuser or so ? */
4411 +  if(!capable(CAP_NET_ADMIN))
4412 +    return -EPERM;
4413 +
4414 +  if(sock->type != SOCK_RAW)
4415 +    return -ESOCKTNOSUPPORT;
4416 +
4417 +  if(protocol != htons(ETH_P_ALL))
4418 +    return -EPROTONOSUPPORT;
4419 +
4420 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
4421 +  MOD_INC_USE_COUNT;
4422 +#endif
4423 +
4424 +  err = -ENOMEM;
4425 +
4426 +  // BD: -- broke this out to keep it more simple and clear as to what the
4427 +  // options are.
4428 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4429 +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11))
4430 +  sk = sk_alloc(PF_RING, GFP_KERNEL, 1, NULL);
4431 +#endif
4432 +#endif
4433 +
4434 +  // BD: API changed in 2.6.12, ref:
4435 +  // http://svn.clkao.org/svnweb/linux/revision/?rev=28201
4436 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
4437 +  sk = sk_alloc(PF_RING, GFP_ATOMIC, &ring_proto, 1);
4438 +#endif
4439 +
4440 +  if (sk == NULL)
4441 +    goto out;
4442 +
4443 +  sock->ops = &ring_ops;
4444 +  sock_init_data(sock, sk);
4445 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4446 +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11))
4447 +  sk_set_owner(sk, THIS_MODULE);
4448 +#endif
4449 +#endif
4450 +
4451 +  err = -ENOMEM;
4452 +  ring_sk(sk) = ring_sk_datatype(kmalloc(sizeof(*pfr), GFP_KERNEL));
4453 +
4454 +  if (!(pfr = ring_sk(sk))) {
4455 +    sk_free(sk);
4456 +    goto out;
4457 +  }
4458 +  memset(pfr, 0, sizeof(*pfr));
4459 +  init_waitqueue_head(&pfr->ring_slots_waitqueue);
4460 +  pfr->ring_index_lock = RW_LOCK_UNLOCKED;
4461 +  atomic_set(&pfr->num_ring_slots_waiters, 0);
4462 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4463 +  sk->sk_family       = PF_RING;
4464 +  sk->sk_destruct     = ring_sock_destruct;
4465 +#else
4466 +  sk->family          = PF_RING;
4467 +  sk->destruct        = ring_sock_destruct;
4468 +  sk->num             = protocol;
4469 +#endif
4470 +
4471 +  ring_insert(sk);
4472 +
4473 +#if defined(RING_DEBUG)
4474 +  printk("RING: ring_create() - created\n");
4475 +#endif
4476 +
4477 +  return(0);
4478 + out:
4479 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
4480 +  MOD_DEC_USE_COUNT;
4481 +#endif
4482 +  return err;
4483 +}
4484 +
4485 +/* *********************************************** */
4486 +
4487 +static int ring_release(struct socket *sock)
4488 +{
4489 +  struct sock *sk = sock->sk;
4490 +  struct ring_opt *pfr = ring_sk(sk);
4491 +
4492 +  if(!sk)
4493 +    return 0;
4494 +
4495 +#if defined(RING_DEBUG)
4496 +  printk("RING: called ring_release\n");
4497 +#endif
4498 +
4499 +#if defined(RING_DEBUG)
4500 +  printk("RING: ring_release entered\n");
4501 +#endif
4502 +
4503 +  ring_remove(sk);
4504 +
4505 +  sock_orphan(sk);
4506 +  sock->sk = NULL;
4507 +
4508 +  /* Free the ring buffer */
4509 +  if(pfr->ring_memory) {
4510 +    struct page *page, *page_end;
4511 +
4512 +    page_end = virt_to_page(pfr->ring_memory + (PAGE_SIZE << pfr->order) - 1);
4513 +    for(page = virt_to_page(pfr->ring_memory); page <= page_end; page++)
4514 +      ClearPageReserved(page);
4515 +
4516 +    free_pages(pfr->ring_memory, pfr->order);
4517 +  }
4518 +
4519 +  kfree(pfr);
4520 +  ring_sk(sk) = NULL;
4521 +
4522 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4523 +  skb_queue_purge(&sk->sk_write_queue);
4524 +#endif
4525 +  sock_put(sk);
4526 +
4527 +#if defined(RING_DEBUG)
4528 +  printk("RING: ring_release leaving\n");
4529 +#endif
4530 +
4531 +  return 0;
4532 +}
4533 +
4534 +/* ********************************** */
4535 +/*
4536 + * We create a ring for this socket and bind it to the specified device
4537 + */
4538 +static int packet_ring_bind(struct sock *sk, struct net_device *dev)
4539 +{
4540 +  u_int the_slot_len;
4541 +  u_int32_t tot_mem;
4542 +  struct ring_opt *pfr = ring_sk(sk);
4543 +  struct page *page, *page_end;
4544 +
4545 +  if(!dev) return(-1);
4546 +
4547 +#if defined(RING_DEBUG)
4548 +  printk("RING: packet_ring_bind(%s) called\n", dev->name);
4549 +#endif
4550 +
4551 +  /* **********************************************
4552 +
4553 +  *************************************
4554 +  *                                   *
4555 +  *        FlowSlotInfo               *
4556 +  *                                   *
4557 +  ************************************* <-+
4558 +  *        FlowSlot                   *   |
4559 +  *************************************   |
4560 +  *        FlowSlot                   *   |
4561 +  *************************************   +- num_slots
4562 +  *        FlowSlot                   *   |
4563 +  *************************************   |
4564 +  *        FlowSlot                   *   |
4565 +  ************************************* <-+
4566 +
4567 +  ********************************************** */
4568 +
4569 +  the_slot_len = sizeof(u_char)    /* flowSlot.slot_state */
4570 +    + sizeof(u_short) /* flowSlot.slot_len   */
4571 +    + bucket_len      /* flowSlot.bucket     */;
4572 +
4573 +  tot_mem = sizeof(FlowSlotInfo) + num_slots*the_slot_len;
4574 +
4575 +  /*
4576 +    Calculate the value of the order parameter used later.
4577 +    See http://www.linuxjournal.com/article.php?sid=1133
4578 +  */
4579 +  for(pfr->order = 0;(PAGE_SIZE << pfr->order) < tot_mem; pfr->order++)  ;
4580 +
4581 +  /*
4582 +    We now try to allocate the memory as required. If we fail
4583 +    we try to allocate a smaller amount or memory (hence a
4584 +    smaller ring).
4585 +  */
4586 +  while((pfr->ring_memory = __get_free_pages(GFP_ATOMIC, pfr->order)) == 0)
4587 +    if(pfr->order-- == 0)
4588 +      break;
4589 +
4590 +  if(pfr->order == 0) {
4591 +#if defined(RING_DEBUG)
4592 +    printk("ERROR: not enough memory\n");
4593 +#endif
4594 +    return(-1);
4595 +  } else {
4596 +#if defined(RING_DEBUG)
4597 +    printk("RING: succesfully allocated %lu KB [tot_mem=%d][order=%ld]\n",
4598 +          PAGE_SIZE >> (10 - pfr->order), tot_mem, pfr->order);
4599 +#endif
4600 +  }
4601 +
4602 +  tot_mem = PAGE_SIZE << pfr->order;
4603 +  memset((char*)pfr->ring_memory, 0, tot_mem);
4604 +
4605 +  /* Now we need to reserve the pages */
4606 +  page_end = virt_to_page(pfr->ring_memory + (PAGE_SIZE << pfr->order) - 1);
4607 +  for(page = virt_to_page(pfr->ring_memory); page <= page_end; page++)
4608 +    SetPageReserved(page);
4609 +
4610 +  pfr->slots_info = (FlowSlotInfo*)pfr->ring_memory;
4611 +  pfr->ring_slots = (char*)(pfr->ring_memory+sizeof(FlowSlotInfo));
4612 +
4613 +  pfr->slots_info->version     = RING_FLOWSLOT_VERSION;
4614 +  pfr->slots_info->slot_len    = the_slot_len;
4615 +  pfr->slots_info->tot_slots   = (tot_mem-sizeof(FlowSlotInfo))/the_slot_len;
4616 +  pfr->slots_info->tot_mem     = tot_mem;
4617 +  pfr->slots_info->sample_rate = sample_rate;
4618 +
4619 +#if defined(RING_DEBUG)
4620 +  printk("RING: allocated %d slots [slot_len=%d][tot_mem=%u]\n",
4621 +        pfr->slots_info->tot_slots, pfr->slots_info->slot_len,
4622 +        pfr->slots_info->tot_mem);
4623 +#endif
4624 +
4625 +#ifdef RING_MAGIC
4626 +  {
4627 +    int i;
4628 +
4629 +    for(i=0; i<pfr->slots_info->tot_slots; i++) {
4630 +      unsigned long idx = i*pfr->slots_info->slot_len;
4631 +      FlowSlot *slot = (FlowSlot*)&pfr->ring_slots[idx];
4632 +      slot->magic = RING_MAGIC_VALUE; slot->slot_state = 0;
4633 +    }
4634 +  }
4635 +#endif
4636 +
4637 +  pfr->insert_page_id = 1, pfr->insert_slot_id = 0;
4638 +
4639 +  /*
4640 +    IMPORTANT
4641 +    Leave this statement here as last one. In fact when
4642 +    the ring_netdev != NULL the socket is ready to be used.
4643 +  */
4644 +  pfr->ring_netdev = dev;
4645 +
4646 +  return(0);
4647 +}
4648 +
4649 +/* ************************************* */
4650 +
4651 +/* Bind to a device */
4652 +static int ring_bind(struct socket *sock,
4653 +                    struct sockaddr *sa, int addr_len)
4654 +{
4655 +  struct sock *sk=sock->sk;
4656 +  struct net_device *dev = NULL;
4657 +
4658 +#if defined(RING_DEBUG)
4659 +  printk("RING: ring_bind() called\n");
4660 +#endif
4661 +
4662 +  /*
4663 +   *   Check legality
4664 +   */
4665 +  if (addr_len != sizeof(struct sockaddr))
4666 +    return -EINVAL;
4667 +  if (sa->sa_family != PF_RING)
4668 +    return -EINVAL;
4669 +
4670 +  /* Safety check: add trailing zero if missing */
4671 +  sa->sa_data[sizeof(sa->sa_data)-1] = '\0';
4672 +
4673 +#if defined(RING_DEBUG)
4674 +  printk("RING: searching device %s\n", sa->sa_data);
4675 +#endif
4676 +
4677 +  if((dev = __dev_get_by_name(sa->sa_data)) == NULL) {
4678 +#if defined(RING_DEBUG)
4679 +    printk("RING: search failed\n");
4680 +#endif
4681 +    return(-EINVAL);
4682 +  } else
4683 +    return(packet_ring_bind(sk, dev));
4684 +}
4685 +
4686 +/* ************************************* */
4687 +
4688 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4689 +
4690 +volatile void* virt_to_kseg(volatile void* address) {
4691 +  pte_t *pte;
4692 +  pud_t *pud;
4693 +  unsigned long addr = (unsigned long)address;
4694 +                      
4695 +  pud = pud_offset(pgd_offset_k((unsigned long) address),
4696 +                  (unsigned long) address);
4697 +
4698 +  /*
4699 +     High-memory support courtesy of
4700 +     Brad Doctor <bdoctor@ps-ax.com>
4701 +  */
4702 +#if defined(CONFIG_X86_PAE) && (!defined(CONFIG_NOHIGHMEM))
4703 +  pte = pte_offset_map(pmd_offset(pud, addr), addr);
4704 +#else
4705 +  pte = pmd_offset_map(pud, addr);
4706 +#endif
4707 +
4708 +  return((volatile void*)pte_page(*pte));
4709 +}
4710 +
4711 +#else /* 2.4 */
4712 +
4713 +/* http://www.scs.ch/~frey/linux/memorymap.html */
4714 +volatile void *virt_to_kseg(volatile void *address)
4715 +{
4716 +  pgd_t *pgd; pmd_t *pmd; pte_t *ptep, pte;
4717 +  unsigned long va, ret = 0UL;
4718 +
4719 +  va=VMALLOC_VMADDR((unsigned long)address);
4720 +
4721 +  /* get the page directory. Use the kernel memory map. */
4722 +  pgd = pgd_offset_k(va);
4723 +
4724 +  /* check whether we found an entry */
4725 +  if (!pgd_none(*pgd))
4726 +    {
4727 +      /* get the page middle directory */
4728 +      pmd = pmd_offset(pgd, va);
4729 +      /* check whether we found an entry */
4730 +      if (!pmd_none(*pmd))
4731 +       {
4732 +         /* get a pointer to the page table entry */
4733 +         ptep = pte_offset(pmd, va);
4734 +         pte = *ptep;
4735 +         /* check for a valid page */
4736 +         if (pte_present(pte))
4737 +           {
4738 +             /* get the address the page is refering to */
4739 +             ret = (unsigned long)page_address(pte_page(pte));
4740 +             /* add the offset within the page to the page address */
4741 +             ret |= (va & (PAGE_SIZE -1));
4742 +           }
4743 +       }
4744 +    }
4745 +  return((volatile void *)ret);
4746 +}
4747 +#endif
4748 +
4749 +/* ************************************* */
4750 +
4751 +static int ring_mmap(struct file *file,
4752 +                    struct socket *sock,
4753 +                    struct vm_area_struct *vma)
4754 +{
4755 +  struct sock *sk = sock->sk;
4756 +  struct ring_opt *pfr = ring_sk(sk);
4757 +  unsigned long size, start;
4758 +  u_int pagesToMap;
4759 +  char *ptr;
4760 +
4761 +#if defined(RING_DEBUG)
4762 +  printk("RING: ring_mmap() called\n");
4763 +#endif
4764 +
4765 +  if(pfr->ring_memory == 0) {
4766 +#if defined(RING_DEBUG)
4767 +    printk("RING: ring_mmap() failed: mapping area to an unbound socket\n");
4768 +#endif
4769 +    return -EINVAL;
4770 +  }
4771 +
4772 +  size = (unsigned long)(vma->vm_end-vma->vm_start);
4773 +
4774 +  if(size % PAGE_SIZE) {
4775 +#if defined(RING_DEBUG)
4776 +    printk("RING: ring_mmap() failed: len is not multiple of PAGE_SIZE\n");
4777 +#endif
4778 +    return(-EINVAL);
4779 +  }
4780 +
4781 +  /* if userspace tries to mmap beyond end of our buffer, fail */
4782 +  if(size > pfr->slots_info->tot_mem) {
4783 +#if defined(RING_DEBUG)
4784 +    printk("proc_mmap() failed: area too large [%ld > %d]\n", size, pfr->slots_info->tot_mem);
4785 +#endif
4786 +    return(-EINVAL);
4787 +  }
4788 +
4789 +  pagesToMap = size/PAGE_SIZE;
4790 +
4791 +#if defined(RING_DEBUG)
4792 +  printk("RING: ring_mmap() called. %d pages to map\n", pagesToMap);
4793 +#endif
4794 +
4795 +#if defined(RING_DEBUG)
4796 +  printk("RING: mmap [slot_len=%d][tot_slots=%d] for ring on device %s\n",
4797 +        pfr->slots_info->slot_len, pfr->slots_info->tot_slots,
4798 +        pfr->ring_netdev->name);
4799 +#endif
4800 +
4801 +  /* we do not want to have this area swapped out, lock it */
4802 +  vma->vm_flags |= VM_LOCKED;
4803 +  start = vma->vm_start;
4804 +
4805 +  /* Ring slots start from page 1 (page 0 is reserved for FlowSlotInfo) */
4806 +  ptr = (char*)(start+PAGE_SIZE);
4807 +
4808 +  if(remap_page_range(
4809 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4810 +                     vma,
4811 +#endif
4812 +                     start,
4813 +                     __pa(pfr->ring_memory),
4814 +                     PAGE_SIZE*pagesToMap, vma->vm_page_prot)) {
4815 +#if defined(RING_DEBUG)
4816 +    printk("remap_page_range() failed\n");
4817 +#endif
4818 +    return(-EAGAIN);
4819 +  }
4820 +
4821 +#if defined(RING_DEBUG)
4822 +  printk("proc_mmap(pagesToMap=%d): success.\n", pagesToMap);
4823 +#endif
4824 +
4825 +  return 0;
4826 +}
4827 +
4828 +/* ************************************* */
4829 +
4830 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4831 +static int ring_recvmsg(struct kiocb *iocb, struct socket *sock,
4832 +                       struct msghdr *msg, size_t len, int flags)
4833 +#else
4834 +  static int ring_recvmsg(struct socket *sock, struct msghdr *msg, int len,
4835 +                         int flags, struct scm_cookie *scm)
4836 +#endif
4837 +{
4838 +  FlowSlot* slot;
4839 +  struct ring_opt *pfr = ring_sk(sock->sk);
4840 +  u_int32_t queued_pkts, num_loops = 0;
4841 +
4842 +#if defined(RING_DEBUG)
4843 +  printk("ring_recvmsg called\n");
4844 +#endif
4845 +
4846 +  slot = get_remove_slot(pfr);
4847 +
4848 +  while((queued_pkts = num_queued_pkts(pfr)) < MIN_QUEUED_PKTS) {
4849 +    wait_event_interruptible(pfr->ring_slots_waitqueue, 1);
4850 +
4851 +#if defined(RING_DEBUG)
4852 +    printk("-> ring_recvmsg returning %d [queued_pkts=%d][num_loops=%d]\n",
4853 +          slot->slot_state, queued_pkts, num_loops);
4854 +#endif
4855 +
4856 +    if(queued_pkts > 0) {
4857 +      if(num_loops++ > MAX_QUEUE_LOOPS)
4858 +       break;
4859 +    }
4860 +  }
4861 +
4862 +#if defined(RING_DEBUG)
4863 +  if(slot != NULL)
4864 +    printk("ring_recvmsg is returning [queued_pkts=%d][num_loops=%d]\n",
4865 +          queued_pkts, num_loops);
4866 +#endif
4867 +
4868 +  return(queued_pkts);
4869 +}
4870 +
4871 +/* ************************************* */
4872 +
4873 +unsigned int ring_poll(struct file * file,
4874 +                      struct socket *sock, poll_table *wait)
4875 +{
4876 +  FlowSlot* slot;
4877 +  struct ring_opt *pfr = ring_sk(sock->sk);
4878 +
4879 +#if defined(RING_DEBUG)
4880 +  printk("poll called\n");
4881 +#endif
4882 +
4883 +  slot = get_remove_slot(pfr);
4884 +
4885 +  if((slot != NULL) && (slot->slot_state == 0))
4886 +    poll_wait(file, &pfr->ring_slots_waitqueue, wait);
4887 +
4888 +#if defined(RING_DEBUG)
4889 +  printk("poll returning %d\n", slot->slot_state);
4890 +#endif
4891 +
4892 +  if((slot != NULL) && (slot->slot_state == 1))
4893 +    return(POLLIN | POLLRDNORM);
4894 +  else
4895 +    return(0);
4896 +}
4897 +
4898 +/* ************************************* */
4899 +
4900 +int add_to_cluster_list(struct ring_cluster *el,
4901 +                       struct sock *sock) {
4902 +
4903 +  if(el->num_cluster_elements == CLUSTER_LEN)
4904 +    return(-1); /* Cluster full */
4905 +
4906 +  ring_sk_datatype(ring_sk(sock))->cluster_id = el->cluster_id;
4907 +  el->sk[el->num_cluster_elements] = sock;
4908 +  el->num_cluster_elements++;
4909 +  return(0);
4910 +}
4911 +
4912 +/* ************************************* */
4913 +
4914 +int remove_from_cluster_list(struct ring_cluster *el,
4915 +                            struct sock *sock) {
4916 +  int i, j;
4917 +
4918 +  for(i=0; i<CLUSTER_LEN; i++)
4919 +    if(el->sk[i] == sock) {
4920 +      el->num_cluster_elements--;
4921 +
4922 +      if(el->num_cluster_elements > 0) {
4923 +       /* The cluster contains other elements */
4924 +       for(j=i; j<CLUSTER_LEN-1; j++)
4925 +         el->sk[j] = el->sk[j+1];
4926 +
4927 +       el->sk[CLUSTER_LEN-1] = NULL;
4928 +      } else {
4929 +       /* Empty cluster */
4930 +       memset(el->sk, 0, sizeof(el->sk));
4931 +      }
4932 +
4933 +      return(0);
4934 +    }
4935 +
4936 +  return(-1); /* Not found */
4937 +}
4938 +
4939 +/* ************************************* */
4940 +
4941 +static int remove_from_cluster(struct sock *sock,
4942 +                              struct ring_opt *pfr)
4943 +{
4944 +  struct ring_cluster *el;
4945 +
4946 +#if defined(RING_DEBUG)
4947 +  printk("--> remove_from_cluster(%d)\n", pfr->cluster_id);
4948 +#endif
4949 +
4950 +  if(pfr->cluster_id == 0 /* 0 = No Cluster */)
4951 +    return(0); /* Noting to do */
4952 +
4953 +  el = ring_cluster_list;
4954 +
4955 +  while(el != NULL) {
4956 +    if(el->cluster_id == pfr->cluster_id) {
4957 +      return(remove_from_cluster_list(el, sock));
4958 +    } else
4959 +      el = el->next;
4960 +  }
4961 +
4962 +  return(-EINVAL); /* Not found */
4963 +}
4964 +
4965 +/* ************************************* */
4966 +
4967 +static int add_to_cluster(struct sock *sock,
4968 +                         struct ring_opt *pfr,
4969 +                         u_short cluster_id)
4970 +{
4971 +  struct ring_cluster *el;
4972 +
4973 +#ifndef RING_DEBUG
4974 +  printk("--> add_to_cluster(%d)\n", cluster_id);
4975 +#endif
4976 +
4977 +  if(cluster_id == 0 /* 0 = No Cluster */) return(-EINVAL);
4978 +
4979 +  if(pfr->cluster_id != 0)
4980 +    remove_from_cluster(sock, pfr);
4981 +
4982 +  el = ring_cluster_list;
4983 +
4984 +  while(el != NULL) {
4985 +    if(el->cluster_id == cluster_id) {
4986 +      return(add_to_cluster_list(el, sock));
4987 +    } else
4988 +      el = el->next;
4989 +  }
4990 +
4991 +  /* There's no existing cluster. We need to create one */
4992 +  if((el = kmalloc(sizeof(struct ring_cluster), GFP_KERNEL)) == NULL)
4993 +    return(-ENOMEM);
4994 +
4995 +  el->cluster_id = cluster_id;
4996 +  el->num_cluster_elements = 1;
4997 +  el->hashing_mode = cluster_per_flow; /* Default */
4998 +  el->hashing_id   = 0;
4999 +
5000 +  memset(el->sk, 0, sizeof(el->sk));
5001 +  el->sk[0] = sock;
5002 +  el->next = ring_cluster_list;
5003 +  ring_cluster_list = el;
5004 +  pfr->cluster_id = cluster_id;
5005 +
5006 +  return(0); /* 0 = OK */
5007 +}
5008 +
5009 +/* ************************************* */
5010 +
5011 +/* Code taken/inspired from core/sock.c */
5012 +static int ring_setsockopt(struct socket *sock,
5013 +                          int level, int optname,
5014 +                          char *optval, int optlen)
5015 +{
5016 +  struct ring_opt *pfr = ring_sk(sock->sk);
5017 +  int val, found, ret = 0;
5018 +  u_int cluster_id;
5019 +  char devName[8];
5020 +
5021 +  if((optlen<sizeof(int)) || (pfr == NULL))
5022 +    return(-EINVAL);
5023 +
5024 +  if (get_user(val, (int *)optval))
5025 +    return -EFAULT;
5026 +
5027 +  found = 1;
5028 +
5029 +  switch(optname)
5030 +    {
5031 +    case SO_ATTACH_FILTER:
5032 +      ret = -EINVAL;
5033 +      if (optlen == sizeof(struct sock_fprog)) {
5034 +       unsigned int fsize;
5035 +       struct sock_fprog fprog;
5036 +       struct sk_filter *filter;
5037 +
5038 +       ret = -EFAULT;
5039 +
5040 +       /*
5041 +         NOTE
5042 +
5043 +         Do not call copy_from_user within a held
5044 +         splinlock (e.g. ring_mgmt_lock) as this caused
5045 +         problems when certain debugging was enabled under
5046 +         2.6.5 -- including hard lockups of the machine.
5047 +       */
5048 +       if(copy_from_user(&fprog, optval, sizeof(fprog)))
5049 +         break;
5050 +
5051 +       fsize = sizeof(struct sock_filter) * fprog.len;
5052 +       filter = kmalloc(fsize, GFP_KERNEL);
5053 +
5054 +       if(filter == NULL) {
5055 +         ret = -ENOMEM;
5056 +         break;
5057 +       }
5058 +
5059 +       if(copy_from_user(filter->insns, fprog.filter, fsize))
5060 +         break;
5061 +
5062 +       filter->len = fprog.len;
5063 +
5064 +       if(sk_chk_filter(filter->insns, filter->len) != 0) {
5065 +         /* Bad filter specified */
5066 +         kfree(filter);
5067 +         pfr->bpfFilter = NULL;
5068 +         break;
5069 +       }
5070 +
5071 +       /* get the lock, set the filter, release the lock */
5072 +       write_lock(&ring_mgmt_lock);
5073 +       pfr->bpfFilter = filter;
5074 +       write_unlock(&ring_mgmt_lock);
5075 +      }
5076 +      ret = 0;
5077 +      break;
5078 +
5079 +    case SO_DETACH_FILTER:
5080 +      write_lock(&ring_mgmt_lock);
5081 +      found = 1;
5082 +      if(pfr->bpfFilter != NULL) {
5083 +       kfree(pfr->bpfFilter);
5084 +       pfr->bpfFilter = NULL;
5085 +       write_unlock(&ring_mgmt_lock);
5086 +       break;
5087 +      }
5088 +      ret = -ENONET;
5089 +      break;
5090 +
5091 +    case SO_ADD_TO_CLUSTER:
5092 +      if (optlen!=sizeof(val))
5093 +       return -EINVAL;
5094 +
5095 +      if (copy_from_user(&cluster_id, optval, sizeof(cluster_id)))
5096 +       return -EFAULT;
5097 +
5098 +      write_lock(&ring_mgmt_lock);
5099 +      ret = add_to_cluster(sock->sk, pfr, cluster_id);
5100 +      write_unlock(&ring_mgmt_lock);
5101 +      break;
5102 +
5103 +    case SO_REMOVE_FROM_CLUSTER:
5104 +      write_lock(&ring_mgmt_lock);
5105 +      ret = remove_from_cluster(sock->sk, pfr);
5106 +      write_unlock(&ring_mgmt_lock);
5107 +      break;
5108 +
5109 +    case SO_SET_REFLECTOR:
5110 +      if(optlen >= (sizeof(devName)-1))
5111 +       return -EINVAL;
5112 +
5113 +      if(optlen > 0) {
5114 +       if(copy_from_user(devName, optval, optlen))
5115 +         return -EFAULT;
5116 +      }
5117 +
5118 +      devName[optlen] = '\0';
5119 +
5120 +#if defined(RING_DEBUG)
5121 +      printk("+++ SO_SET_REFLECTOR(%s)\n", devName);
5122 +#endif
5123 +
5124 +      write_lock(&ring_mgmt_lock);
5125 +      pfr->reflector_dev = dev_get_by_name(devName);
5126 +      write_unlock(&ring_mgmt_lock);
5127 +
5128 +#if defined(RING_DEBUG)
5129 +      if(pfr->reflector_dev != NULL)
5130 +       printk("SO_SET_REFLECTOR(%s): succeded\n", devName);
5131 +      else
5132 +       printk("SO_SET_REFLECTOR(%s): device unknown\n", devName);
5133 +#endif
5134 +      break;
5135 +
5136 +    default:
5137 +      found = 0;
5138 +      break;
5139 +    }
5140 +
5141 +  if(found)
5142 +    return(ret);
5143 +  else
5144 +    return(sock_setsockopt(sock, level, optname, optval, optlen));
5145 +}
5146 +
5147 +/* ************************************* */
5148 +
5149 +static int ring_ioctl(struct socket *sock,
5150 +                     unsigned int cmd, unsigned long arg)
5151 +{
5152 +  switch(cmd)
5153 +    {
5154 +    case SIOCGIFFLAGS:
5155 +    case SIOCSIFFLAGS:
5156 +    case SIOCGIFCONF:
5157 +    case SIOCGIFMETRIC:
5158 +    case SIOCSIFMETRIC:
5159 +    case SIOCGIFMEM:
5160 +    case SIOCSIFMEM:
5161 +    case SIOCGIFMTU:
5162 +    case SIOCSIFMTU:
5163 +    case SIOCSIFLINK:
5164 +    case SIOCGIFHWADDR:
5165 +    case SIOCSIFHWADDR:
5166 +    case SIOCSIFMAP:
5167 +    case SIOCGIFMAP:
5168 +    case SIOCSIFSLAVE:
5169 +    case SIOCGIFSLAVE:
5170 +    case SIOCGIFINDEX:
5171 +    case SIOCGIFNAME:
5172 +    case SIOCGIFCOUNT:
5173 +    case SIOCSIFHWBROADCAST:
5174 +      return(dev_ioctl(cmd,(void *) arg));
5175 +
5176 +    default:
5177 +      return -EOPNOTSUPP;
5178 +    }
5179 +
5180 +  return 0;
5181 +}
5182 +
5183 +/* ************************************* */
5184 +
5185 +static struct proto_ops ring_ops = {
5186 +  .family      =       PF_RING,
5187 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5188 +  .owner       =       THIS_MODULE,
5189 +#endif
5190 +
5191 +  /* Operations that make no sense on ring sockets. */
5192 +  .connect     =       sock_no_connect,
5193 +  .socketpair  =       sock_no_socketpair,
5194 +  .accept      =       sock_no_accept,
5195 +  .getname     =       sock_no_getname,
5196 +  .listen      =       sock_no_listen,
5197 +  .shutdown    =       sock_no_shutdown,
5198 +  .sendpage    =       sock_no_sendpage,
5199 +  .sendmsg     =       sock_no_sendmsg,
5200 +  .getsockopt  =       sock_no_getsockopt,
5201 +
5202 +  /* Now the operations that really occur. */
5203 +  .release     =       ring_release,
5204 +  .bind                =       ring_bind,
5205 +  .mmap                =       ring_mmap,
5206 +  .poll                =       ring_poll,
5207 +  .setsockopt  =       ring_setsockopt,
5208 +  .ioctl       =       ring_ioctl,
5209 +  .recvmsg     =       ring_recvmsg,
5210 +};
5211 +
5212 +/* ************************************ */
5213 +
5214 +static struct net_proto_family ring_family_ops = {
5215 +  .family      =       PF_RING,
5216 +  .create      =       ring_create,
5217 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5218 +  .owner       =       THIS_MODULE,
5219 +#endif
5220 +};
5221 +
5222 +// BD: API changed in 2.6.12, ref:
5223 +// http://svn.clkao.org/svnweb/linux/revision/?rev=28201
5224 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
5225 +static struct proto ring_proto = {
5226 +  .name                =       "PF_RING",
5227 +  .owner       =       THIS_MODULE,
5228 +  .obj_size    =       sizeof(struct sock),
5229 +};
5230 +#endif
5231 +
5232 +/* ************************************ */
5233 +
5234 +static void __exit ring_exit(void)
5235 +{
5236 +  struct list_head *ptr;
5237 +  struct ring_element *entry;
5238 +
5239 +  for(ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
5240 +    entry = list_entry(ptr, struct ring_element, list);
5241 +    kfree(entry);
5242 +  }
5243 +
5244 +  while(ring_cluster_list != NULL) {
5245 +    struct ring_cluster *next = ring_cluster_list->next;
5246 +    kfree(ring_cluster_list);
5247 +    ring_cluster_list = next;
5248 +  }
5249 +
5250 +  set_skb_ring_handler(NULL);
5251 +  set_buffer_ring_handler(NULL);
5252 +  sock_unregister(PF_RING);
5253 +
5254 +  printk("PF_RING shut down.\n");
5255 +}
5256 +
5257 +/* ************************************ */
5258 +
5259 +static int __init ring_init(void)
5260 +{
5261 +  printk("Welcome to PF_RING %s\n(C) 2004 L.Deri <deri@ntop.org>\n",
5262 +        RING_VERSION);
5263 +
5264 +  INIT_LIST_HEAD(&ring_table);
5265 +  ring_cluster_list = NULL;
5266 +
5267 +  sock_register(&ring_family_ops);
5268 +
5269 +  set_skb_ring_handler(skb_ring_handler);
5270 +  set_buffer_ring_handler(buffer_ring_handler);
5271 +
5272 +  if(get_buffer_ring_handler() != buffer_ring_handler) {
5273 +    printk("PF_RING: set_buffer_ring_handler FAILED\n");
5274 +
5275 +    set_skb_ring_handler(NULL);
5276 +    set_buffer_ring_handler(NULL);
5277 +    sock_unregister(PF_RING);
5278 +    return -1;
5279 +  } else {
5280 +    printk("PF_RING: bucket length    %d bytes\n", bucket_len);
5281 +    printk("PF_RING: ring slots       %d\n", num_slots);
5282 +    printk("PF_RING: sample rate      %d [1=no sampling]\n", sample_rate);
5283 +    printk("PF_RING: capture TX       %s\n",
5284 +          enable_tx_capture ? "Yes [RX+TX]" : "No [RX only]");
5285 +    printk("PF_RING: transparent mode %s\n",
5286 +          transparent_mode ? "Yes" : "No");
5287 +
5288 +    printk("PF_RING initialized correctly.\n");
5289 +    return 0;
5290 +  }
5291 +}
5292 +
5293 +module_init(ring_init);
5294 +module_exit(ring_exit);
5295 +MODULE_LICENSE("GPL");
5296 +
5297 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5298 +MODULE_ALIAS_NETPROTO(PF_RING);
5299 +#endif