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