1 #ifndef __LINUX_MROUTE_BASE_H
2 #define __LINUX_MROUTE_BASE_H
4 #include <linux/netdevice.h>
5 #include <linux/rhashtable-types.h>
6 #include <linux/spinlock.h>
7 #include <net/net_namespace.h>
9 #include <net/fib_notifier.h>
10 #include <net/ip_fib.h>
13 * struct vif_device - interface representor for multicast routing
14 * @dev: network device being used
15 * @dev_tracker: refcount tracker for @dev reference
16 * @bytes_in: statistic; bytes ingressing
17 * @bytes_out: statistic; bytes egresing
18 * @pkt_in: statistic; packets ingressing
19 * @pkt_out: statistic; packets egressing
20 * @rate_limit: Traffic shaping (NI)
21 * @threshold: TTL threshold
22 * @flags: Control flags
23 * @link: Physical interface index
24 * @dev_parent_id: device parent id
25 * @local: Local address
26 * @remote: Remote address for tunnels
29 struct net_device *dev;
30 netdevice_tracker dev_tracker;
31 unsigned long bytes_in, bytes_out;
32 unsigned long pkt_in, pkt_out;
33 unsigned long rate_limit;
34 unsigned char threshold;
38 /* Currently only used by ipmr */
39 struct netdev_phys_item_id dev_parent_id;
43 struct vif_entry_notifier_info {
44 struct fib_notifier_info info;
45 struct net_device *dev;
46 unsigned short vif_index;
47 unsigned short vif_flags;
51 static inline int mr_call_vif_notifier(struct notifier_block *nb,
52 unsigned short family,
53 enum fib_event_type event_type,
54 struct vif_device *vif,
55 unsigned short vif_index, u32 tb_id,
56 struct netlink_ext_ack *extack)
58 struct vif_entry_notifier_info info = {
64 .vif_index = vif_index,
65 .vif_flags = vif->flags,
69 return call_fib_notifier(nb, event_type, &info.info);
72 static inline int mr_call_vif_notifiers(struct net *net,
73 unsigned short family,
74 enum fib_event_type event_type,
75 struct vif_device *vif,
76 unsigned short vif_index, u32 tb_id,
77 unsigned int *ipmr_seq)
79 struct vif_entry_notifier_info info = {
84 .vif_index = vif_index,
85 .vif_flags = vif->flags,
91 return call_fib_notifiers(net, event_type, &info.info);
95 /* This one is nasty; value is defined in uapi using different symbols for
96 * mroute and morute6 but both map into same 32.
101 #define VIF_EXISTS(_mrt, _idx) (!!((_mrt)->vif_table[_idx].dev))
104 * MFC_STATIC - the entry was added statically (not by a routing daemon)
105 * MFC_OFFLOAD - the entry was offloaded to the hardware
109 MFC_OFFLOAD = BIT(1),
113 * struct mr_mfc - common multicast routing entries
114 * @mnode: rhashtable list
115 * @mfc_parent: source interface (iif)
116 * @mfc_flags: entry flags
117 * @expires: unresolved entry expire time
118 * @unresolved: unresolved cached skbs
119 * @last_assert: time of last assert
120 * @minvif: minimum VIF id
121 * @maxvif: maximum VIF id
122 * @bytes: bytes that have passed for this entry
123 * @pkt: packets that have passed for this entry
124 * @wrong_if: number of wrong source interface hits
125 * @lastuse: time of last use of the group (traffic or update)
126 * @ttls: OIF TTL threshold array
127 * @refcount: reference count for this entry
128 * @list: global entry list
129 * @rcu: used for entry destruction
130 * @free: Operation used for freeing an entry under RCU
133 struct rhlist_head mnode;
134 unsigned short mfc_parent;
139 unsigned long expires;
140 struct sk_buff_head unresolved;
143 unsigned long last_assert;
148 unsigned long wrong_if;
149 unsigned long lastuse;
150 unsigned char ttls[MAXVIFS];
154 struct list_head list;
156 void (*free)(struct rcu_head *head);
159 static inline void mr_cache_put(struct mr_mfc *c)
161 if (refcount_dec_and_test(&c->mfc_un.res.refcount))
162 call_rcu(&c->rcu, c->free);
165 static inline void mr_cache_hold(struct mr_mfc *c)
167 refcount_inc(&c->mfc_un.res.refcount);
170 struct mfc_entry_notifier_info {
171 struct fib_notifier_info info;
176 static inline int mr_call_mfc_notifier(struct notifier_block *nb,
177 unsigned short family,
178 enum fib_event_type event_type,
179 struct mr_mfc *mfc, u32 tb_id,
180 struct netlink_ext_ack *extack)
182 struct mfc_entry_notifier_info info = {
191 return call_fib_notifier(nb, event_type, &info.info);
194 static inline int mr_call_mfc_notifiers(struct net *net,
195 unsigned short family,
196 enum fib_event_type event_type,
197 struct mr_mfc *mfc, u32 tb_id,
198 unsigned int *ipmr_seq)
200 struct mfc_entry_notifier_info info = {
210 return call_fib_notifiers(net, event_type, &info.info);
216 * struct mr_table_ops - callbacks and info for protocol-specific ops
217 * @rht_params: parameters for accessing the MFC hash
218 * @cmparg_any: a hash key to be used for matching on (*,*) routes
220 struct mr_table_ops {
221 const struct rhashtable_params *rht_params;
226 * struct mr_table - a multicast routing table
227 * @list: entry within a list of multicast routing tables
228 * @net: net where this table belongs
229 * @ops: protocol specific operations
230 * @id: identifier of the table
231 * @mroute_sk: socket associated with the table
232 * @ipmr_expire_timer: timer for handling unresolved routes
233 * @mfc_unres_queue: list of unresolved MFC entries
234 * @vif_table: array containing all possible vifs
235 * @mfc_hash: Hash table of all resolved routes for easy lookup
236 * @mfc_cache_list: list of resovled routes for possible traversal
237 * @maxvif: Identifier of highest value vif currently in use
238 * @cache_resolve_queue_len: current size of unresolved queue
239 * @mroute_do_assert: Whether to inform userspace on wrong ingress
240 * @mroute_do_pim: Whether to receive IGMP PIMv1
241 * @mroute_reg_vif_num: PIM-device vif index
244 struct list_head list;
246 struct mr_table_ops ops;
248 struct sock __rcu *mroute_sk;
249 struct timer_list ipmr_expire_timer;
250 struct list_head mfc_unres_queue;
251 struct vif_device vif_table[MAXVIFS];
252 struct rhltable mfc_hash;
253 struct list_head mfc_cache_list;
255 atomic_t cache_resolve_queue_len;
256 bool mroute_do_assert;
258 bool mroute_do_wrvifwhole;
259 int mroute_reg_vif_num;
262 #ifdef CONFIG_IP_MROUTE_COMMON
263 void vif_device_init(struct vif_device *v,
264 struct net_device *dev,
265 unsigned long rate_limit,
266 unsigned char threshold,
267 unsigned short flags,
268 unsigned short get_iflink_mask);
271 mr_table_alloc(struct net *net, u32 id,
272 struct mr_table_ops *ops,
273 void (*expire_func)(struct timer_list *t),
274 void (*table_set)(struct mr_table *mrt,
277 /* These actually return 'struct mr_mfc *', but to avoid need for explicit
278 * castings they simply return void.
280 void *mr_mfc_find_parent(struct mr_table *mrt,
281 void *hasharg, int parent);
282 void *mr_mfc_find_any_parent(struct mr_table *mrt, int vifi);
283 void *mr_mfc_find_any(struct mr_table *mrt, int vifi, void *hasharg);
285 int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
286 struct mr_mfc *c, struct rtmsg *rtm);
287 int mr_table_dump(struct mr_table *mrt, struct sk_buff *skb,
288 struct netlink_callback *cb,
289 int (*fill)(struct mr_table *mrt, struct sk_buff *skb,
290 u32 portid, u32 seq, struct mr_mfc *c,
292 spinlock_t *lock, struct fib_dump_filter *filter);
293 int mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
294 struct mr_table *(*iter)(struct net *net,
295 struct mr_table *mrt),
296 int (*fill)(struct mr_table *mrt,
298 u32 portid, u32 seq, struct mr_mfc *c,
300 spinlock_t *lock, struct fib_dump_filter *filter);
302 int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family,
303 int (*rules_dump)(struct net *net,
304 struct notifier_block *nb,
305 struct netlink_ext_ack *extack),
306 struct mr_table *(*mr_iter)(struct net *net,
307 struct mr_table *mrt),
308 rwlock_t *mrt_lock, struct netlink_ext_ack *extack);
310 static inline void vif_device_init(struct vif_device *v,
311 struct net_device *dev,
312 unsigned long rate_limit,
313 unsigned char threshold,
314 unsigned short flags,
315 unsigned short get_iflink_mask)
319 static inline void *mr_mfc_find_parent(struct mr_table *mrt,
320 void *hasharg, int parent)
325 static inline void *mr_mfc_find_any_parent(struct mr_table *mrt,
331 static inline struct mr_mfc *mr_mfc_find_any(struct mr_table *mrt,
332 int vifi, void *hasharg)
337 static inline int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
338 struct mr_mfc *c, struct rtmsg *rtm)
344 mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
345 struct mr_table *(*iter)(struct net *net,
346 struct mr_table *mrt),
347 int (*fill)(struct mr_table *mrt,
349 u32 portid, u32 seq, struct mr_mfc *c,
351 spinlock_t *lock, struct fib_dump_filter *filter)
356 static inline int mr_dump(struct net *net, struct notifier_block *nb,
357 unsigned short family,
358 int (*rules_dump)(struct net *net,
359 struct notifier_block *nb,
360 struct netlink_ext_ack *extack),
361 struct mr_table *(*mr_iter)(struct net *net,
362 struct mr_table *mrt),
363 rwlock_t *mrt_lock, struct netlink_ext_ack *extack)
369 static inline void *mr_mfc_find(struct mr_table *mrt, void *hasharg)
371 return mr_mfc_find_parent(mrt, hasharg, -1);
374 #ifdef CONFIG_PROC_FS
376 struct seq_net_private p;
377 struct mr_table *mrt;
382 struct seq_net_private p;
383 struct mr_table *mrt;
384 struct list_head *cache;
386 /* Lock protecting the mr_table's unresolved queue */
390 #ifdef CONFIG_IP_MROUTE_COMMON
391 void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter, loff_t pos);
392 void *mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos);
394 static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
396 return *pos ? mr_vif_seq_idx(seq_file_net(seq),
397 seq->private, *pos - 1)
401 /* These actually return 'struct mr_mfc *', but to avoid need for explicit
402 * castings they simply return void.
404 void *mr_mfc_seq_idx(struct net *net,
405 struct mr_mfc_iter *it, loff_t pos);
406 void *mr_mfc_seq_next(struct seq_file *seq, void *v,
409 static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
410 struct mr_table *mrt, spinlock_t *lock)
412 struct mr_mfc_iter *it = seq->private;
418 return *pos ? mr_mfc_seq_idx(seq_file_net(seq),
419 seq->private, *pos - 1)
423 static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)
425 struct mr_mfc_iter *it = seq->private;
426 struct mr_table *mrt = it->mrt;
428 if (it->cache == &mrt->mfc_unres_queue)
429 spin_unlock_bh(it->lock);
430 else if (it->cache == &mrt->mfc_cache_list)
434 static inline void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter,
440 static inline void *mr_vif_seq_next(struct seq_file *seq,
441 void *v, loff_t *pos)
446 static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
451 static inline void *mr_mfc_seq_idx(struct net *net,
452 struct mr_mfc_iter *it, loff_t pos)
457 static inline void *mr_mfc_seq_next(struct seq_file *seq, void *v,
463 static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
464 struct mr_table *mrt, spinlock_t *lock)
469 static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)