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