blob: fd436cdd47256b615abc72d50268873708487bcd [file] [log] [blame]
Yuval Mintz6853f212018-02-28 23:29:29 +02001#ifndef __LINUX_MROUTE_BASE_H
2#define __LINUX_MROUTE_BASE_H
3
4#include <linux/netdevice.h>
NeilBrown0eb71a92018-06-18 12:52:50 +10005#include <linux/rhashtable-types.h>
Yuval Mintzc8d61962018-02-28 23:29:36 +02006#include <linux/spinlock.h>
Yuval Mintzb70432f2018-02-28 23:29:32 +02007#include <net/net_namespace.h>
8#include <net/sock.h>
Yuval Mintzbc67a0d2018-03-26 15:01:31 +03009#include <net/fib_notifier.h>
Yuval Mintz6853f212018-02-28 23:29:29 +020010
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 */
26struct 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
Yuval Mintzbc67a0d2018-03-26 15:01:31 +030040struct 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
48static 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
69static 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
Yuval Mintzb70432f2018-02-28 23:29:32 +020092#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
Yuval Mintz889cd832018-02-28 23:29:38 +0200101/* 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 */
105enum {
106 MFC_STATIC = BIT(0),
107 MFC_OFFLOAD = BIT(1),
108};
109
Yuval Mintzb70432f2018-02-28 23:29:32 +0200110/**
Yuval Mintz494fff52018-02-28 23:29:34 +0200111 * 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
Yuval Mintz8c13af22018-03-26 15:01:36 +0300128 * @free: Operation used for freeing an entry under RCU
Yuval Mintz494fff52018-02-28 23:29:34 +0200129 */
130struct mr_mfc {
131 struct rhlist_head mnode;
132 unsigned short mfc_parent;
133 int mfc_flags;
134
135 union {
136 struct {
137 unsigned long expires;
138 struct sk_buff_head unresolved;
139 } unres;
140 struct {
141 unsigned long last_assert;
142 int minvif;
143 int maxvif;
144 unsigned long bytes;
145 unsigned long pkt;
146 unsigned long wrong_if;
147 unsigned long lastuse;
148 unsigned char ttls[MAXVIFS];
149 refcount_t refcount;
150 } res;
151 } mfc_un;
152 struct list_head list;
153 struct rcu_head rcu;
Yuval Mintz8c13af22018-03-26 15:01:36 +0300154 void (*free)(struct rcu_head *head);
Yuval Mintz494fff52018-02-28 23:29:34 +0200155};
156
Yuval Mintz8c13af22018-03-26 15:01:36 +0300157static inline void mr_cache_put(struct mr_mfc *c)
158{
159 if (refcount_dec_and_test(&c->mfc_un.res.refcount))
160 call_rcu(&c->rcu, c->free);
161}
162
163static inline void mr_cache_hold(struct mr_mfc *c)
164{
165 refcount_inc(&c->mfc_un.res.refcount);
166}
167
Yuval Mintz54c4cad2018-03-26 15:01:32 +0300168struct mfc_entry_notifier_info {
169 struct fib_notifier_info info;
170 struct mr_mfc *mfc;
171 u32 tb_id;
172};
173
174static inline int mr_call_mfc_notifier(struct notifier_block *nb,
175 struct net *net,
176 unsigned short family,
177 enum fib_event_type event_type,
178 struct mr_mfc *mfc, u32 tb_id)
179{
180 struct mfc_entry_notifier_info info = {
181 .info = {
182 .family = family,
183 .net = net,
184 },
185 .mfc = mfc,
186 .tb_id = tb_id
187 };
188
189 return call_fib_notifier(nb, net, event_type, &info.info);
190}
191
192static inline int mr_call_mfc_notifiers(struct net *net,
193 unsigned short family,
194 enum fib_event_type event_type,
195 struct mr_mfc *mfc, u32 tb_id,
196 unsigned int *ipmr_seq)
197{
198 struct mfc_entry_notifier_info info = {
199 .info = {
200 .family = family,
201 .net = net,
202 },
203 .mfc = mfc,
204 .tb_id = tb_id
205 };
206
207 ASSERT_RTNL();
208 (*ipmr_seq)++;
209 return call_fib_notifiers(net, event_type, &info.info);
210}
211
Yuval Mintz845c9a72018-02-28 23:29:35 +0200212struct mr_table;
213
214/**
215 * struct mr_table_ops - callbacks and info for protocol-specific ops
216 * @rht_params: parameters for accessing the MFC hash
217 * @cmparg_any: a hash key to be used for matching on (*,*) routes
218 */
219struct mr_table_ops {
220 const struct rhashtable_params *rht_params;
221 void *cmparg_any;
222};
223
Yuval Mintz494fff52018-02-28 23:29:34 +0200224/**
Yuval Mintzb70432f2018-02-28 23:29:32 +0200225 * struct mr_table - a multicast routing table
226 * @list: entry within a list of multicast routing tables
227 * @net: net where this table belongs
Yuval Mintz845c9a72018-02-28 23:29:35 +0200228 * @ops: protocol specific operations
Yuval Mintzb70432f2018-02-28 23:29:32 +0200229 * @id: identifier of the table
230 * @mroute_sk: socket associated with the table
231 * @ipmr_expire_timer: timer for handling unresolved routes
232 * @mfc_unres_queue: list of unresolved MFC entries
233 * @vif_table: array containing all possible vifs
234 * @mfc_hash: Hash table of all resolved routes for easy lookup
235 * @mfc_cache_list: list of resovled routes for possible traversal
236 * @maxvif: Identifier of highest value vif currently in use
237 * @cache_resolve_queue_len: current size of unresolved queue
238 * @mroute_do_assert: Whether to inform userspace on wrong ingress
239 * @mroute_do_pim: Whether to receive IGMP PIMv1
240 * @mroute_reg_vif_num: PIM-device vif index
241 */
242struct mr_table {
243 struct list_head list;
244 possible_net_t net;
Yuval Mintz845c9a72018-02-28 23:29:35 +0200245 struct mr_table_ops ops;
Yuval Mintzb70432f2018-02-28 23:29:32 +0200246 u32 id;
247 struct sock __rcu *mroute_sk;
248 struct timer_list ipmr_expire_timer;
249 struct list_head mfc_unres_queue;
250 struct vif_device vif_table[MAXVIFS];
251 struct rhltable mfc_hash;
252 struct list_head mfc_cache_list;
253 int maxvif;
254 atomic_t cache_resolve_queue_len;
255 bool mroute_do_assert;
256 bool mroute_do_pim;
257 int mroute_reg_vif_num;
258};
259
Yuval Mintz6853f212018-02-28 23:29:29 +0200260#ifdef CONFIG_IP_MROUTE_COMMON
261void vif_device_init(struct vif_device *v,
262 struct net_device *dev,
263 unsigned long rate_limit,
264 unsigned char threshold,
265 unsigned short flags,
266 unsigned short get_iflink_mask);
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200267
268struct mr_table *
269mr_table_alloc(struct net *net, u32 id,
Yuval Mintz845c9a72018-02-28 23:29:35 +0200270 struct mr_table_ops *ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200271 void (*expire_func)(struct timer_list *t),
272 void (*table_set)(struct mr_table *mrt,
273 struct net *net));
Yuval Mintz845c9a72018-02-28 23:29:35 +0200274
275/* These actually return 'struct mr_mfc *', but to avoid need for explicit
276 * castings they simply return void.
277 */
278void *mr_mfc_find_parent(struct mr_table *mrt,
279 void *hasharg, int parent);
280void *mr_mfc_find_any_parent(struct mr_table *mrt, int vifi);
281void *mr_mfc_find_any(struct mr_table *mrt, int vifi, void *hasharg);
282
Yuval Mintz7b0db852018-02-28 23:29:39 +0200283int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
284 struct mr_mfc *c, struct rtmsg *rtm);
285int mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
286 struct mr_table *(*iter)(struct net *net,
287 struct mr_table *mrt),
288 int (*fill)(struct mr_table *mrt,
289 struct sk_buff *skb,
290 u32 portid, u32 seq, struct mr_mfc *c,
291 int cmd, int flags),
292 spinlock_t *lock);
Yuval Mintzcdc9f942018-03-26 15:01:33 +0300293
294int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family,
295 int (*rules_dump)(struct net *net,
296 struct notifier_block *nb),
297 struct mr_table *(*mr_iter)(struct net *net,
298 struct mr_table *mrt),
299 rwlock_t *mrt_lock);
Yuval Mintz6853f212018-02-28 23:29:29 +0200300#else
301static inline void vif_device_init(struct vif_device *v,
302 struct net_device *dev,
303 unsigned long rate_limit,
304 unsigned char threshold,
305 unsigned short flags,
306 unsigned short get_iflink_mask)
307{
308}
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200309
Yuval Mintz845c9a72018-02-28 23:29:35 +0200310static inline void *mr_mfc_find_parent(struct mr_table *mrt,
311 void *hasharg, int parent)
312{
313 return NULL;
314}
315
316static inline void *mr_mfc_find_any_parent(struct mr_table *mrt,
317 int vifi)
318{
319 return NULL;
320}
321
322static inline struct mr_mfc *mr_mfc_find_any(struct mr_table *mrt,
323 int vifi, void *hasharg)
324{
325 return NULL;
326}
Yuval Mintz7b0db852018-02-28 23:29:39 +0200327
328static inline int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
329 struct mr_mfc *c, struct rtmsg *rtm)
330{
331 return -EINVAL;
332}
333
334static inline int
335mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
336 struct mr_table *(*iter)(struct net *net,
337 struct mr_table *mrt),
338 int (*fill)(struct mr_table *mrt,
339 struct sk_buff *skb,
340 u32 portid, u32 seq, struct mr_mfc *c,
341 int cmd, int flags),
342 spinlock_t *lock)
343{
344 return -EINVAL;
345}
Yuval Mintzcdc9f942018-03-26 15:01:33 +0300346
347static inline int mr_dump(struct net *net, struct notifier_block *nb,
348 unsigned short family,
349 int (*rules_dump)(struct net *net,
350 struct notifier_block *nb),
351 struct mr_table *(*mr_iter)(struct net *net,
352 struct mr_table *mrt),
353 rwlock_t *mrt_lock)
354{
355 return -EINVAL;
356}
Yuval Mintz6853f212018-02-28 23:29:29 +0200357#endif
Yuval Mintz845c9a72018-02-28 23:29:35 +0200358
359static inline void *mr_mfc_find(struct mr_table *mrt, void *hasharg)
360{
361 return mr_mfc_find_parent(mrt, hasharg, -1);
362}
Yuval Mintzc8d61962018-02-28 23:29:36 +0200363
364#ifdef CONFIG_PROC_FS
Yuval Mintz3feda6b2018-02-28 23:29:37 +0200365struct mr_vif_iter {
366 struct seq_net_private p;
367 struct mr_table *mrt;
368 int ct;
369};
370
Yuval Mintzc8d61962018-02-28 23:29:36 +0200371struct mr_mfc_iter {
372 struct seq_net_private p;
373 struct mr_table *mrt;
374 struct list_head *cache;
375
376 /* Lock protecting the mr_table's unresolved queue */
377 spinlock_t *lock;
378};
379
380#ifdef CONFIG_IP_MROUTE_COMMON
Yuval Mintz3feda6b2018-02-28 23:29:37 +0200381void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter, loff_t pos);
382void *mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos);
383
384static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
385{
386 return *pos ? mr_vif_seq_idx(seq_file_net(seq),
387 seq->private, *pos - 1)
388 : SEQ_START_TOKEN;
389}
390
Yuval Mintzc8d61962018-02-28 23:29:36 +0200391/* These actually return 'struct mr_mfc *', but to avoid need for explicit
392 * castings they simply return void.
393 */
394void *mr_mfc_seq_idx(struct net *net,
395 struct mr_mfc_iter *it, loff_t pos);
396void *mr_mfc_seq_next(struct seq_file *seq, void *v,
397 loff_t *pos);
398
399static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
400 struct mr_table *mrt, spinlock_t *lock)
401{
402 struct mr_mfc_iter *it = seq->private;
403
404 it->mrt = mrt;
405 it->cache = NULL;
406 it->lock = lock;
407
408 return *pos ? mr_mfc_seq_idx(seq_file_net(seq),
409 seq->private, *pos - 1)
410 : SEQ_START_TOKEN;
411}
412
413static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)
414{
415 struct mr_mfc_iter *it = seq->private;
416 struct mr_table *mrt = it->mrt;
417
418 if (it->cache == &mrt->mfc_unres_queue)
419 spin_unlock_bh(it->lock);
420 else if (it->cache == &mrt->mfc_cache_list)
421 rcu_read_unlock();
422}
423#else
Yuval Mintz3feda6b2018-02-28 23:29:37 +0200424static inline void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter,
425 loff_t pos)
426{
427 return NULL;
428}
429
430static inline void *mr_vif_seq_next(struct seq_file *seq,
431 void *v, loff_t *pos)
432{
433 return NULL;
434}
435
436static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
437{
438 return NULL;
439}
440
Yuval Mintzc8d61962018-02-28 23:29:36 +0200441static inline void *mr_mfc_seq_idx(struct net *net,
442 struct mr_mfc_iter *it, loff_t pos)
443{
444 return NULL;
445}
446
447static inline void *mr_mfc_seq_next(struct seq_file *seq, void *v,
448 loff_t *pos)
449{
450 return NULL;
451}
452
453static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
454 struct mr_table *mrt, spinlock_t *lock)
455{
456 return NULL;
457}
458
459static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)
460{
461}
462#endif
463#endif
Yuval Mintz6853f212018-02-28 23:29:29 +0200464#endif