[NETFILTER]: H.323 helper: fix possible NULL-ptr dereference
[linux-2.6] / net / ipv4 / multipath_drr.c
1 /*
2  *              Device round robin policy for multipath.
3  *
4  *
5  * Version:     $Id: multipath_drr.c,v 1.1.2.1 2004/09/16 07:42:34 elueck Exp $
6  *
7  * Authors:     Einar Lueck <elueck@de.ibm.com><lkml@einar-lueck.de>
8  *
9  *              This program is free software; you can redistribute it and/or
10  *              modify it under the terms of the GNU General Public License
11  *              as published by the Free Software Foundation; either version
12  *              2 of the License, or (at your option) any later version.
13  */
14
15 #include <asm/system.h>
16 #include <asm/uaccess.h>
17 #include <linux/types.h>
18 #include <linux/sched.h>
19 #include <linux/errno.h>
20 #include <linux/timer.h>
21 #include <linux/mm.h>
22 #include <linux/kernel.h>
23 #include <linux/fcntl.h>
24 #include <linux/stat.h>
25 #include <linux/socket.h>
26 #include <linux/in.h>
27 #include <linux/inet.h>
28 #include <linux/netdevice.h>
29 #include <linux/inetdevice.h>
30 #include <linux/igmp.h>
31 #include <linux/proc_fs.h>
32 #include <linux/seq_file.h>
33 #include <linux/module.h>
34 #include <linux/mroute.h>
35 #include <linux/init.h>
36 #include <net/ip.h>
37 #include <net/protocol.h>
38 #include <linux/skbuff.h>
39 #include <net/sock.h>
40 #include <net/icmp.h>
41 #include <net/udp.h>
42 #include <net/raw.h>
43 #include <linux/notifier.h>
44 #include <linux/if_arp.h>
45 #include <linux/netfilter_ipv4.h>
46 #include <net/ipip.h>
47 #include <net/checksum.h>
48 #include <net/ip_mp_alg.h>
49
50 struct multipath_device {
51         int             ifi; /* interface index of device */
52         atomic_t        usecount;
53         int             allocated;
54 };
55
56 #define MULTIPATH_MAX_DEVICECANDIDATES 10
57
58 static struct multipath_device state[MULTIPATH_MAX_DEVICECANDIDATES];
59 static DEFINE_SPINLOCK(state_lock);
60
61 static int inline __multipath_findslot(void)
62 {
63         int i;
64
65         for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++) {
66                 if (state[i].allocated == 0)
67                         return i;
68         }
69         return -1;
70 }
71
72 static int inline __multipath_finddev(int ifindex)
73 {
74         int i;
75
76         for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++) {
77                 if (state[i].allocated != 0 &&
78                     state[i].ifi == ifindex)
79                         return i;
80         }
81         return -1;
82 }
83
84 static int drr_dev_event(struct notifier_block *this,
85                          unsigned long event, void *ptr)
86 {
87         struct net_device *dev = ptr;
88         int devidx;
89
90         switch (event) {
91         case NETDEV_UNREGISTER:
92         case NETDEV_DOWN:
93                 spin_lock_bh(&state_lock);
94
95                 devidx = __multipath_finddev(dev->ifindex);
96                 if (devidx != -1) {
97                         state[devidx].allocated = 0;
98                         state[devidx].ifi = 0;
99                         atomic_set(&state[devidx].usecount, 0);
100                 }
101
102                 spin_unlock_bh(&state_lock);
103                 break;
104         };
105
106         return NOTIFY_DONE;
107 }
108
109 static struct notifier_block drr_dev_notifier = {
110         .notifier_call  = drr_dev_event,
111 };
112
113
114 static void drr_safe_inc(atomic_t *usecount)
115 {
116         int n;
117
118         atomic_inc(usecount);
119
120         n = atomic_read(usecount);
121         if (n <= 0) {
122                 int i;
123
124                 spin_lock_bh(&state_lock);
125
126                 for (i = 0; i < MULTIPATH_MAX_DEVICECANDIDATES; i++)
127                         atomic_set(&state[i].usecount, 0);
128
129                 spin_unlock_bh(&state_lock);
130         }
131 }
132
133 static void drr_select_route(const struct flowi *flp,
134                              struct rtable *first, struct rtable **rp)
135 {
136         struct rtable *nh, *result, *cur_min;
137         int min_usecount = -1; 
138         int devidx = -1;
139         int cur_min_devidx = -1;
140
141         /* 1. make sure all alt. nexthops have the same GC related data */
142         /* 2. determine the new candidate to be returned */
143         result = NULL;
144         cur_min = NULL;
145         for (nh = rcu_dereference(first); nh;
146              nh = rcu_dereference(nh->u.rt_next)) {
147                 if ((nh->u.dst.flags & DST_BALANCED) != 0 &&
148                     multipath_comparekeys(&nh->fl, flp)) {
149                         int nh_ifidx = nh->u.dst.dev->ifindex;
150
151                         nh->u.dst.lastuse = jiffies;
152                         nh->u.dst.__use++;
153                         if (result != NULL)
154                                 continue;
155
156                         /* search for the output interface */
157
158                         /* this is not SMP safe, only add/remove are
159                          * SMP safe as wrong usecount updates have no big
160                          * impact
161                          */
162                         devidx = __multipath_finddev(nh_ifidx);
163                         if (devidx == -1) {
164                                 /* add the interface to the array 
165                                  * SMP safe
166                                  */
167                                 spin_lock_bh(&state_lock);
168
169                                 /* due to SMP: search again */
170                                 devidx = __multipath_finddev(nh_ifidx);
171                                 if (devidx == -1) {
172                                         /* add entry for device */
173                                         devidx = __multipath_findslot();
174                                         if (devidx == -1) {
175                                                 /* unlikely but possible */
176                                                 continue;
177                                         }
178
179                                         state[devidx].allocated = 1;
180                                         state[devidx].ifi = nh_ifidx;
181                                         atomic_set(&state[devidx].usecount, 0);
182                                         min_usecount = 0;
183                                 }
184
185                                 spin_unlock_bh(&state_lock);
186                         }
187
188                         if (min_usecount == 0) {
189                                 /* if the device has not been used it is
190                                  * the primary target
191                                  */
192                                 drr_safe_inc(&state[devidx].usecount);
193                                 result = nh;
194                         } else {
195                                 int count =
196                                         atomic_read(&state[devidx].usecount);
197
198                                 if (min_usecount == -1 ||
199                                     count < min_usecount) {
200                                         cur_min = nh;
201                                         cur_min_devidx = devidx;
202                                         min_usecount = count;
203                                 }
204                         }
205                 }
206         }
207
208         if (!result) {
209                 if (cur_min) {
210                         drr_safe_inc(&state[cur_min_devidx].usecount);
211                         result = cur_min;
212                 } else {
213                         result = first;
214                 }
215         }
216
217         *rp = result;
218 }
219
220 static struct ip_mp_alg_ops drr_ops = {
221         .mp_alg_select_route    =       drr_select_route,
222 };
223
224 static int __init drr_init(void)
225 {
226         int err = register_netdevice_notifier(&drr_dev_notifier);
227
228         if (err)
229                 return err;
230
231         err = multipath_alg_register(&drr_ops, IP_MP_ALG_DRR);
232         if (err)
233                 goto fail;
234
235         return 0;
236
237 fail:
238         unregister_netdevice_notifier(&drr_dev_notifier);
239         return err;
240 }
241
242 static void __exit drr_exit(void)
243 {
244         unregister_netdevice_notifier(&drr_dev_notifier);
245         multipath_alg_unregister(&drr_ops, IP_MP_ALG_DRR);
246 }
247
248 module_init(drr_init);
249 module_exit(drr_exit);
250 MODULE_LICENSE("GPL");