blob: 2c594686c05e6b95e796f6cf89326f5fdde604b6 [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>
Yuval Mintzb70432f2018-02-28 23:29:32 +02005#include <linux/rhashtable.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
128 */
129struct 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
Yuval Mintz54c4cad2018-03-26 15:01:32 +0300155struct mfc_entry_notifier_info {
156 struct fib_notifier_info info;
157 struct mr_mfc *mfc;
158 u32 tb_id;
159};
160
161static inline int mr_call_mfc_notifier(struct notifier_block *nb,
162 struct net *net,
163 unsigned short family,
164 enum fib_event_type event_type,
165 struct mr_mfc *mfc, u32 tb_id)
166{
167 struct mfc_entry_notifier_info info = {
168 .info = {
169 .family = family,
170 .net = net,
171 },
172 .mfc = mfc,
173 .tb_id = tb_id
174 };
175
176 return call_fib_notifier(nb, net, event_type, &info.info);
177}
178
179static inline int mr_call_mfc_notifiers(struct net *net,
180 unsigned short family,
181 enum fib_event_type event_type,
182 struct mr_mfc *mfc, u32 tb_id,
183 unsigned int *ipmr_seq)
184{
185 struct mfc_entry_notifier_info info = {
186 .info = {
187 .family = family,
188 .net = net,
189 },
190 .mfc = mfc,
191 .tb_id = tb_id
192 };
193
194 ASSERT_RTNL();
195 (*ipmr_seq)++;
196 return call_fib_notifiers(net, event_type, &info.info);
197}
198
Yuval Mintz845c9a72018-02-28 23:29:35 +0200199struct mr_table;
200
201/**
202 * struct mr_table_ops - callbacks and info for protocol-specific ops
203 * @rht_params: parameters for accessing the MFC hash
204 * @cmparg_any: a hash key to be used for matching on (*,*) routes
205 */
206struct mr_table_ops {
207 const struct rhashtable_params *rht_params;
208 void *cmparg_any;
209};
210
Yuval Mintz494fff52018-02-28 23:29:34 +0200211/**
Yuval Mintzb70432f2018-02-28 23:29:32 +0200212 * struct mr_table - a multicast routing table
213 * @list: entry within a list of multicast routing tables
214 * @net: net where this table belongs
Yuval Mintz845c9a72018-02-28 23:29:35 +0200215 * @ops: protocol specific operations
Yuval Mintzb70432f2018-02-28 23:29:32 +0200216 * @id: identifier of the table
217 * @mroute_sk: socket associated with the table
218 * @ipmr_expire_timer: timer for handling unresolved routes
219 * @mfc_unres_queue: list of unresolved MFC entries
220 * @vif_table: array containing all possible vifs
221 * @mfc_hash: Hash table of all resolved routes for easy lookup
222 * @mfc_cache_list: list of resovled routes for possible traversal
223 * @maxvif: Identifier of highest value vif currently in use
224 * @cache_resolve_queue_len: current size of unresolved queue
225 * @mroute_do_assert: Whether to inform userspace on wrong ingress
226 * @mroute_do_pim: Whether to receive IGMP PIMv1
227 * @mroute_reg_vif_num: PIM-device vif index
228 */
229struct mr_table {
230 struct list_head list;
231 possible_net_t net;
Yuval Mintz845c9a72018-02-28 23:29:35 +0200232 struct mr_table_ops ops;
Yuval Mintzb70432f2018-02-28 23:29:32 +0200233 u32 id;
234 struct sock __rcu *mroute_sk;
235 struct timer_list ipmr_expire_timer;
236 struct list_head mfc_unres_queue;
237 struct vif_device vif_table[MAXVIFS];
238 struct rhltable mfc_hash;
239 struct list_head mfc_cache_list;
240 int maxvif;
241 atomic_t cache_resolve_queue_len;
242 bool mroute_do_assert;
243 bool mroute_do_pim;
244 int mroute_reg_vif_num;
245};
246
Yuval Mintz6853f212018-02-28 23:29:29 +0200247#ifdef CONFIG_IP_MROUTE_COMMON
248void vif_device_init(struct vif_device *v,
249 struct net_device *dev,
250 unsigned long rate_limit,
251 unsigned char threshold,
252 unsigned short flags,
253 unsigned short get_iflink_mask);
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200254
255struct mr_table *
256mr_table_alloc(struct net *net, u32 id,
Yuval Mintz845c9a72018-02-28 23:29:35 +0200257 struct mr_table_ops *ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200258 void (*expire_func)(struct timer_list *t),
259 void (*table_set)(struct mr_table *mrt,
260 struct net *net));
Yuval Mintz845c9a72018-02-28 23:29:35 +0200261
262/* These actually return 'struct mr_mfc *', but to avoid need for explicit
263 * castings they simply return void.
264 */
265void *mr_mfc_find_parent(struct mr_table *mrt,
266 void *hasharg, int parent);
267void *mr_mfc_find_any_parent(struct mr_table *mrt, int vifi);
268void *mr_mfc_find_any(struct mr_table *mrt, int vifi, void *hasharg);
269
Yuval Mintz7b0db852018-02-28 23:29:39 +0200270int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
271 struct mr_mfc *c, struct rtmsg *rtm);
272int mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
273 struct mr_table *(*iter)(struct net *net,
274 struct mr_table *mrt),
275 int (*fill)(struct mr_table *mrt,
276 struct sk_buff *skb,
277 u32 portid, u32 seq, struct mr_mfc *c,
278 int cmd, int flags),
279 spinlock_t *lock);
Yuval Mintz6853f212018-02-28 23:29:29 +0200280#else
281static inline void vif_device_init(struct vif_device *v,
282 struct net_device *dev,
283 unsigned long rate_limit,
284 unsigned char threshold,
285 unsigned short flags,
286 unsigned short get_iflink_mask)
287{
288}
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200289
Yuval Mintz845c9a72018-02-28 23:29:35 +0200290static inline void *
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200291mr_table_alloc(struct net *net, u32 id,
Yuval Mintz845c9a72018-02-28 23:29:35 +0200292 struct mr_table_ops *ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200293 void (*expire_func)(struct timer_list *t),
294 void (*table_set)(struct mr_table *mrt,
295 struct net *net))
296{
297 return NULL;
298}
Yuval Mintz845c9a72018-02-28 23:29:35 +0200299
300static inline void *mr_mfc_find_parent(struct mr_table *mrt,
301 void *hasharg, int parent)
302{
303 return NULL;
304}
305
306static inline void *mr_mfc_find_any_parent(struct mr_table *mrt,
307 int vifi)
308{
309 return NULL;
310}
311
312static inline struct mr_mfc *mr_mfc_find_any(struct mr_table *mrt,
313 int vifi, void *hasharg)
314{
315 return NULL;
316}
Yuval Mintz7b0db852018-02-28 23:29:39 +0200317
318static inline int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
319 struct mr_mfc *c, struct rtmsg *rtm)
320{
321 return -EINVAL;
322}
323
324static inline int
325mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
326 struct mr_table *(*iter)(struct net *net,
327 struct mr_table *mrt),
328 int (*fill)(struct mr_table *mrt,
329 struct sk_buff *skb,
330 u32 portid, u32 seq, struct mr_mfc *c,
331 int cmd, int flags),
332 spinlock_t *lock)
333{
334 return -EINVAL;
335}
Yuval Mintz6853f212018-02-28 23:29:29 +0200336#endif
Yuval Mintz845c9a72018-02-28 23:29:35 +0200337
338static inline void *mr_mfc_find(struct mr_table *mrt, void *hasharg)
339{
340 return mr_mfc_find_parent(mrt, hasharg, -1);
341}
Yuval Mintzc8d61962018-02-28 23:29:36 +0200342
343#ifdef CONFIG_PROC_FS
Yuval Mintz3feda6b2018-02-28 23:29:37 +0200344struct mr_vif_iter {
345 struct seq_net_private p;
346 struct mr_table *mrt;
347 int ct;
348};
349
Yuval Mintzc8d61962018-02-28 23:29:36 +0200350struct mr_mfc_iter {
351 struct seq_net_private p;
352 struct mr_table *mrt;
353 struct list_head *cache;
354
355 /* Lock protecting the mr_table's unresolved queue */
356 spinlock_t *lock;
357};
358
359#ifdef CONFIG_IP_MROUTE_COMMON
Yuval Mintz3feda6b2018-02-28 23:29:37 +0200360void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter, loff_t pos);
361void *mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos);
362
363static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
364{
365 return *pos ? mr_vif_seq_idx(seq_file_net(seq),
366 seq->private, *pos - 1)
367 : SEQ_START_TOKEN;
368}
369
Yuval Mintzc8d61962018-02-28 23:29:36 +0200370/* These actually return 'struct mr_mfc *', but to avoid need for explicit
371 * castings they simply return void.
372 */
373void *mr_mfc_seq_idx(struct net *net,
374 struct mr_mfc_iter *it, loff_t pos);
375void *mr_mfc_seq_next(struct seq_file *seq, void *v,
376 loff_t *pos);
377
378static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
379 struct mr_table *mrt, spinlock_t *lock)
380{
381 struct mr_mfc_iter *it = seq->private;
382
383 it->mrt = mrt;
384 it->cache = NULL;
385 it->lock = lock;
386
387 return *pos ? mr_mfc_seq_idx(seq_file_net(seq),
388 seq->private, *pos - 1)
389 : SEQ_START_TOKEN;
390}
391
392static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)
393{
394 struct mr_mfc_iter *it = seq->private;
395 struct mr_table *mrt = it->mrt;
396
397 if (it->cache == &mrt->mfc_unres_queue)
398 spin_unlock_bh(it->lock);
399 else if (it->cache == &mrt->mfc_cache_list)
400 rcu_read_unlock();
401}
402#else
Yuval Mintz3feda6b2018-02-28 23:29:37 +0200403static inline void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter,
404 loff_t pos)
405{
406 return NULL;
407}
408
409static inline void *mr_vif_seq_next(struct seq_file *seq,
410 void *v, loff_t *pos)
411{
412 return NULL;
413}
414
415static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
416{
417 return NULL;
418}
419
Yuval Mintzc8d61962018-02-28 23:29:36 +0200420static inline void *mr_mfc_seq_idx(struct net *net,
421 struct mr_mfc_iter *it, loff_t pos)
422{
423 return NULL;
424}
425
426static inline void *mr_mfc_seq_next(struct seq_file *seq, void *v,
427 loff_t *pos)
428{
429 return NULL;
430}
431
432static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
433 struct mr_table *mrt, spinlock_t *lock)
434{
435 return NULL;
436}
437
438static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)
439{
440}
441#endif
442#endif
Yuval Mintz6853f212018-02-28 23:29:29 +0200443#endif