net: fib_notifier: make FIB notifier per-netns
[linux-2.6-microblaze.git] / include / linux / mroute_base.h
1 #ifndef __LINUX_MROUTE_BASE_H
2 #define __LINUX_MROUTE_BASE_H
3
4 #include <linux/netdevice.h>
5 #include <linux/rhashtable-types.h>
6 #include <linux/spinlock.h>
7 #include <net/net_namespace.h>
8 #include <net/sock.h>
9 #include <net/fib_notifier.h>
10 #include <net/ip_fib.h>
11
12 /**
13  * struct vif_device - interface representor for multicast routing
14  * @dev: network device being used
15  * @bytes_in: statistic; bytes ingressing
16  * @bytes_out: statistic; bytes egresing
17  * @pkt_in: statistic; packets ingressing
18  * @pkt_out: statistic; packets egressing
19  * @rate_limit: Traffic shaping (NI)
20  * @threshold: TTL threshold
21  * @flags: Control flags
22  * @link: Physical interface index
23  * @dev_parent_id: device parent id
24  * @local: Local address
25  * @remote: Remote address for tunnels
26  */
27 struct vif_device {
28         struct net_device *dev;
29         unsigned long bytes_in, bytes_out;
30         unsigned long pkt_in, pkt_out;
31         unsigned long rate_limit;
32         unsigned char threshold;
33         unsigned short flags;
34         int link;
35
36         /* Currently only used by ipmr */
37         struct netdev_phys_item_id dev_parent_id;
38         __be32 local, remote;
39 };
40
41 struct vif_entry_notifier_info {
42         struct fib_notifier_info info;
43         struct net_device *dev;
44         unsigned short vif_index;
45         unsigned short vif_flags;
46         u32 tb_id;
47 };
48
49 static inline int mr_call_vif_notifier(struct notifier_block *nb,
50                                        unsigned short family,
51                                        enum fib_event_type event_type,
52                                        struct vif_device *vif,
53                                        unsigned short vif_index, u32 tb_id)
54 {
55         struct vif_entry_notifier_info info = {
56                 .info = {
57                         .family = family,
58                 },
59                 .dev = vif->dev,
60                 .vif_index = vif_index,
61                 .vif_flags = vif->flags,
62                 .tb_id = tb_id,
63         };
64
65         return call_fib_notifier(nb, event_type, &info.info);
66 }
67
68 static inline int mr_call_vif_notifiers(struct net *net,
69                                         unsigned short family,
70                                         enum fib_event_type event_type,
71                                         struct vif_device *vif,
72                                         unsigned short vif_index, u32 tb_id,
73                                         unsigned int *ipmr_seq)
74 {
75         struct vif_entry_notifier_info info = {
76                 .info = {
77                         .family = family,
78                 },
79                 .dev = vif->dev,
80                 .vif_index = vif_index,
81                 .vif_flags = vif->flags,
82                 .tb_id = tb_id,
83         };
84
85         ASSERT_RTNL();
86         (*ipmr_seq)++;
87         return call_fib_notifiers(net, event_type, &info.info);
88 }
89
90 #ifndef MAXVIFS
91 /* This one is nasty; value is defined in uapi using different symbols for
92  * mroute and morute6 but both map into same 32.
93  */
94 #define MAXVIFS 32
95 #endif
96
97 #define VIF_EXISTS(_mrt, _idx) (!!((_mrt)->vif_table[_idx].dev))
98
99 /* mfc_flags:
100  * MFC_STATIC - the entry was added statically (not by a routing daemon)
101  * MFC_OFFLOAD - the entry was offloaded to the hardware
102  */
103 enum {
104         MFC_STATIC = BIT(0),
105         MFC_OFFLOAD = BIT(1),
106 };
107
108 /**
109  * struct mr_mfc - common multicast routing entries
110  * @mnode: rhashtable list
111  * @mfc_parent: source interface (iif)
112  * @mfc_flags: entry flags
113  * @expires: unresolved entry expire time
114  * @unresolved: unresolved cached skbs
115  * @last_assert: time of last assert
116  * @minvif: minimum VIF id
117  * @maxvif: maximum VIF id
118  * @bytes: bytes that have passed for this entry
119  * @pkt: packets that have passed for this entry
120  * @wrong_if: number of wrong source interface hits
121  * @lastuse: time of last use of the group (traffic or update)
122  * @ttls: OIF TTL threshold array
123  * @refcount: reference count for this entry
124  * @list: global entry list
125  * @rcu: used for entry destruction
126  * @free: Operation used for freeing an entry under RCU
127  */
128 struct mr_mfc {
129         struct rhlist_head mnode;
130         unsigned short mfc_parent;
131         int mfc_flags;
132
133         union {
134                 struct {
135                         unsigned long expires;
136                         struct sk_buff_head unresolved;
137                 } unres;
138                 struct {
139                         unsigned long last_assert;
140                         int minvif;
141                         int maxvif;
142                         unsigned long bytes;
143                         unsigned long pkt;
144                         unsigned long wrong_if;
145                         unsigned long lastuse;
146                         unsigned char ttls[MAXVIFS];
147                         refcount_t refcount;
148                 } res;
149         } mfc_un;
150         struct list_head list;
151         struct rcu_head rcu;
152         void (*free)(struct rcu_head *head);
153 };
154
155 static inline void mr_cache_put(struct mr_mfc *c)
156 {
157         if (refcount_dec_and_test(&c->mfc_un.res.refcount))
158                 call_rcu(&c->rcu, c->free);
159 }
160
161 static inline void mr_cache_hold(struct mr_mfc *c)
162 {
163         refcount_inc(&c->mfc_un.res.refcount);
164 }
165
166 struct mfc_entry_notifier_info {
167         struct fib_notifier_info info;
168         struct mr_mfc *mfc;
169         u32 tb_id;
170 };
171
172 static inline int mr_call_mfc_notifier(struct notifier_block *nb,
173                                        unsigned short family,
174                                        enum fib_event_type event_type,
175                                        struct mr_mfc *mfc, u32 tb_id)
176 {
177         struct mfc_entry_notifier_info info = {
178                 .info = {
179                         .family = family,
180                 },
181                 .mfc = mfc,
182                 .tb_id = tb_id
183         };
184
185         return call_fib_notifier(nb, event_type, &info.info);
186 }
187
188 static inline int mr_call_mfc_notifiers(struct net *net,
189                                         unsigned short family,
190                                         enum fib_event_type event_type,
191                                         struct mr_mfc *mfc, u32 tb_id,
192                                         unsigned int *ipmr_seq)
193 {
194         struct mfc_entry_notifier_info info = {
195                 .info = {
196                         .family = family,
197                 },
198                 .mfc = mfc,
199                 .tb_id = tb_id
200         };
201
202         ASSERT_RTNL();
203         (*ipmr_seq)++;
204         return call_fib_notifiers(net, event_type, &info.info);
205 }
206
207 struct mr_table;
208
209 /**
210  * struct mr_table_ops - callbacks and info for protocol-specific ops
211  * @rht_params: parameters for accessing the MFC hash
212  * @cmparg_any: a hash key to be used for matching on (*,*) routes
213  */
214 struct mr_table_ops {
215         const struct rhashtable_params *rht_params;
216         void *cmparg_any;
217 };
218
219 /**
220  * struct mr_table - a multicast routing table
221  * @list: entry within a list of multicast routing tables
222  * @net: net where this table belongs
223  * @ops: protocol specific operations
224  * @id: identifier of the table
225  * @mroute_sk: socket associated with the table
226  * @ipmr_expire_timer: timer for handling unresolved routes
227  * @mfc_unres_queue: list of unresolved MFC entries
228  * @vif_table: array containing all possible vifs
229  * @mfc_hash: Hash table of all resolved routes for easy lookup
230  * @mfc_cache_list: list of resovled routes for possible traversal
231  * @maxvif: Identifier of highest value vif currently in use
232  * @cache_resolve_queue_len: current size of unresolved queue
233  * @mroute_do_assert: Whether to inform userspace on wrong ingress
234  * @mroute_do_pim: Whether to receive IGMP PIMv1
235  * @mroute_reg_vif_num: PIM-device vif index
236  */
237 struct mr_table {
238         struct list_head        list;
239         possible_net_t          net;
240         struct mr_table_ops     ops;
241         u32                     id;
242         struct sock __rcu       *mroute_sk;
243         struct timer_list       ipmr_expire_timer;
244         struct list_head        mfc_unres_queue;
245         struct vif_device       vif_table[MAXVIFS];
246         struct rhltable         mfc_hash;
247         struct list_head        mfc_cache_list;
248         int                     maxvif;
249         atomic_t                cache_resolve_queue_len;
250         bool                    mroute_do_assert;
251         bool                    mroute_do_pim;
252         bool                    mroute_do_wrvifwhole;
253         int                     mroute_reg_vif_num;
254 };
255
256 #ifdef CONFIG_IP_MROUTE_COMMON
257 void vif_device_init(struct vif_device *v,
258                      struct net_device *dev,
259                      unsigned long rate_limit,
260                      unsigned char threshold,
261                      unsigned short flags,
262                      unsigned short get_iflink_mask);
263
264 struct mr_table *
265 mr_table_alloc(struct net *net, u32 id,
266                struct mr_table_ops *ops,
267                void (*expire_func)(struct timer_list *t),
268                void (*table_set)(struct mr_table *mrt,
269                                  struct net *net));
270
271 /* These actually return 'struct mr_mfc *', but to avoid need for explicit
272  * castings they simply return void.
273  */
274 void *mr_mfc_find_parent(struct mr_table *mrt,
275                          void *hasharg, int parent);
276 void *mr_mfc_find_any_parent(struct mr_table *mrt, int vifi);
277 void *mr_mfc_find_any(struct mr_table *mrt, int vifi, void *hasharg);
278
279 int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
280                    struct mr_mfc *c, struct rtmsg *rtm);
281 int mr_table_dump(struct mr_table *mrt, struct sk_buff *skb,
282                   struct netlink_callback *cb,
283                   int (*fill)(struct mr_table *mrt, struct sk_buff *skb,
284                               u32 portid, u32 seq, struct mr_mfc *c,
285                               int cmd, int flags),
286                   spinlock_t *lock, struct fib_dump_filter *filter);
287 int mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
288                      struct mr_table *(*iter)(struct net *net,
289                                               struct mr_table *mrt),
290                      int (*fill)(struct mr_table *mrt,
291                                  struct sk_buff *skb,
292                                  u32 portid, u32 seq, struct mr_mfc *c,
293                                  int cmd, int flags),
294                      spinlock_t *lock, struct fib_dump_filter *filter);
295
296 int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family,
297             int (*rules_dump)(struct net *net,
298                               struct notifier_block *nb),
299             struct mr_table *(*mr_iter)(struct net *net,
300                                         struct mr_table *mrt),
301             rwlock_t *mrt_lock);
302 #else
303 static inline void vif_device_init(struct vif_device *v,
304                                    struct net_device *dev,
305                                    unsigned long rate_limit,
306                                    unsigned char threshold,
307                                    unsigned short flags,
308                                    unsigned short get_iflink_mask)
309 {
310 }
311
312 static inline void *mr_mfc_find_parent(struct mr_table *mrt,
313                                        void *hasharg, int parent)
314 {
315         return NULL;
316 }
317
318 static inline void *mr_mfc_find_any_parent(struct mr_table *mrt,
319                                            int vifi)
320 {
321         return NULL;
322 }
323
324 static inline struct mr_mfc *mr_mfc_find_any(struct mr_table *mrt,
325                                              int vifi, void *hasharg)
326 {
327         return NULL;
328 }
329
330 static inline int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
331                                  struct mr_mfc *c, struct rtmsg *rtm)
332 {
333         return -EINVAL;
334 }
335
336 static inline int
337 mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
338                  struct mr_table *(*iter)(struct net *net,
339                                           struct mr_table *mrt),
340                  int (*fill)(struct mr_table *mrt,
341                              struct sk_buff *skb,
342                              u32 portid, u32 seq, struct mr_mfc *c,
343                              int cmd, int flags),
344                  spinlock_t *lock, struct fib_dump_filter *filter)
345 {
346         return -EINVAL;
347 }
348
349 static inline int mr_dump(struct net *net, struct notifier_block *nb,
350                           unsigned short family,
351                           int (*rules_dump)(struct net *net,
352                                             struct notifier_block *nb),
353                           struct mr_table *(*mr_iter)(struct net *net,
354                                                       struct mr_table *mrt),
355                           rwlock_t *mrt_lock)
356 {
357         return -EINVAL;
358 }
359 #endif
360
361 static inline void *mr_mfc_find(struct mr_table *mrt, void *hasharg)
362 {
363         return mr_mfc_find_parent(mrt, hasharg, -1);
364 }
365
366 #ifdef CONFIG_PROC_FS
367 struct mr_vif_iter {
368         struct seq_net_private p;
369         struct mr_table *mrt;
370         int ct;
371 };
372
373 struct mr_mfc_iter {
374         struct seq_net_private p;
375         struct mr_table *mrt;
376         struct list_head *cache;
377
378         /* Lock protecting the mr_table's unresolved queue */
379         spinlock_t *lock;
380 };
381
382 #ifdef CONFIG_IP_MROUTE_COMMON
383 void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter, loff_t pos);
384 void *mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos);
385
386 static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
387 {
388         return *pos ? mr_vif_seq_idx(seq_file_net(seq),
389                                      seq->private, *pos - 1)
390                     : SEQ_START_TOKEN;
391 }
392
393 /* These actually return 'struct mr_mfc *', but to avoid need for explicit
394  * castings they simply return void.
395  */
396 void *mr_mfc_seq_idx(struct net *net,
397                      struct mr_mfc_iter *it, loff_t pos);
398 void *mr_mfc_seq_next(struct seq_file *seq, void *v,
399                       loff_t *pos);
400
401 static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
402                                      struct mr_table *mrt, spinlock_t *lock)
403 {
404         struct mr_mfc_iter *it = seq->private;
405
406         it->mrt = mrt;
407         it->cache = NULL;
408         it->lock = lock;
409
410         return *pos ? mr_mfc_seq_idx(seq_file_net(seq),
411                                      seq->private, *pos - 1)
412                     : SEQ_START_TOKEN;
413 }
414
415 static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)
416 {
417         struct mr_mfc_iter *it = seq->private;
418         struct mr_table *mrt = it->mrt;
419
420         if (it->cache == &mrt->mfc_unres_queue)
421                 spin_unlock_bh(it->lock);
422         else if (it->cache == &mrt->mfc_cache_list)
423                 rcu_read_unlock();
424 }
425 #else
426 static inline void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter,
427                                    loff_t pos)
428 {
429         return NULL;
430 }
431
432 static inline void *mr_vif_seq_next(struct seq_file *seq,
433                                     void *v, loff_t *pos)
434 {
435         return NULL;
436 }
437
438 static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
439 {
440         return NULL;
441 }
442
443 static inline void *mr_mfc_seq_idx(struct net *net,
444                                    struct mr_mfc_iter *it, loff_t pos)
445 {
446         return NULL;
447 }
448
449 static inline void *mr_mfc_seq_next(struct seq_file *seq, void *v,
450                                     loff_t *pos)
451 {
452         return NULL;
453 }
454
455 static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
456                                      struct mr_table *mrt, spinlock_t *lock)
457 {
458         return NULL;
459 }
460
461 static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)
462 {
463 }
464 #endif
465 #endif
466 #endif