xref: /linux/net/ipv4/ipmr_base.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
16853f21fSYuval Mintz /* Linux multicast routing support
26853f21fSYuval Mintz  * Common logic shared by IPv4 [ipmr] and IPv6 [ip6mr] implementation
36853f21fSYuval Mintz  */
46853f21fSYuval Mintz 
50eb71a9dSNeilBrown #include <linux/rhashtable.h>
66853f21fSYuval Mintz #include <linux/mroute_base.h>
76853f21fSYuval Mintz 
86853f21fSYuval Mintz /* Sets everything common except 'dev', since that is done under locking */
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)96853f21fSYuval Mintz void vif_device_init(struct vif_device *v,
106853f21fSYuval Mintz 		     struct net_device *dev,
116853f21fSYuval Mintz 		     unsigned long rate_limit,
126853f21fSYuval Mintz 		     unsigned char threshold,
136853f21fSYuval Mintz 		     unsigned short flags,
146853f21fSYuval Mintz 		     unsigned short get_iflink_mask)
156853f21fSYuval Mintz {
16ebc31979SEric Dumazet 	RCU_INIT_POINTER(v->dev, NULL);
176853f21fSYuval Mintz 	v->bytes_in = 0;
186853f21fSYuval Mintz 	v->bytes_out = 0;
196853f21fSYuval Mintz 	v->pkt_in = 0;
206853f21fSYuval Mintz 	v->pkt_out = 0;
216853f21fSYuval Mintz 	v->rate_limit = rate_limit;
226853f21fSYuval Mintz 	v->flags = flags;
236853f21fSYuval Mintz 	v->threshold = threshold;
246853f21fSYuval Mintz 	if (v->flags & get_iflink_mask)
256853f21fSYuval Mintz 		v->link = dev_get_iflink(dev);
266853f21fSYuval Mintz 	else
276853f21fSYuval Mintz 		v->link = dev->ifindex;
286853f21fSYuval Mintz }
296853f21fSYuval Mintz EXPORT_SYMBOL(vif_device_init);
300bbbf0e7SYuval Mintz 
310bbbf0e7SYuval Mintz struct mr_table *
mr_table_alloc(struct net * net,u32 id,struct mr_table_ops * ops,void (* expire_func)(struct timer_list * t),void (* table_set)(struct mr_table * mrt,struct net * net))320bbbf0e7SYuval Mintz mr_table_alloc(struct net *net, u32 id,
33845c9a7aSYuval Mintz 	       struct mr_table_ops *ops,
340bbbf0e7SYuval Mintz 	       void (*expire_func)(struct timer_list *t),
350bbbf0e7SYuval Mintz 	       void (*table_set)(struct mr_table *mrt,
360bbbf0e7SYuval Mintz 				 struct net *net))
370bbbf0e7SYuval Mintz {
380bbbf0e7SYuval Mintz 	struct mr_table *mrt;
39e783bb00SSabrina Dubroca 	int err;
400bbbf0e7SYuval Mintz 
410bbbf0e7SYuval Mintz 	mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
420bbbf0e7SYuval Mintz 	if (!mrt)
43e783bb00SSabrina Dubroca 		return ERR_PTR(-ENOMEM);
440bbbf0e7SYuval Mintz 	mrt->id = id;
450bbbf0e7SYuval Mintz 	write_pnet(&mrt->net, net);
460bbbf0e7SYuval Mintz 
47845c9a7aSYuval Mintz 	mrt->ops = *ops;
48e783bb00SSabrina Dubroca 	err = rhltable_init(&mrt->mfc_hash, mrt->ops.rht_params);
49e783bb00SSabrina Dubroca 	if (err) {
5066fb3325SEric Dumazet 		kfree(mrt);
51e783bb00SSabrina Dubroca 		return ERR_PTR(err);
5266fb3325SEric Dumazet 	}
530bbbf0e7SYuval Mintz 	INIT_LIST_HEAD(&mrt->mfc_cache_list);
540bbbf0e7SYuval Mintz 	INIT_LIST_HEAD(&mrt->mfc_unres_queue);
550bbbf0e7SYuval Mintz 
560bbbf0e7SYuval Mintz 	timer_setup(&mrt->ipmr_expire_timer, expire_func, 0);
570bbbf0e7SYuval Mintz 
580bbbf0e7SYuval Mintz 	mrt->mroute_reg_vif_num = -1;
590bbbf0e7SYuval Mintz 	table_set(mrt, net);
600bbbf0e7SYuval Mintz 	return mrt;
610bbbf0e7SYuval Mintz }
620bbbf0e7SYuval Mintz EXPORT_SYMBOL(mr_table_alloc);
63845c9a7aSYuval Mintz 
mr_mfc_find_parent(struct mr_table * mrt,void * hasharg,int parent)64845c9a7aSYuval Mintz void *mr_mfc_find_parent(struct mr_table *mrt, void *hasharg, int parent)
65845c9a7aSYuval Mintz {
66845c9a7aSYuval Mintz 	struct rhlist_head *tmp, *list;
67845c9a7aSYuval Mintz 	struct mr_mfc *c;
68845c9a7aSYuval Mintz 
69845c9a7aSYuval Mintz 	list = rhltable_lookup(&mrt->mfc_hash, hasharg, *mrt->ops.rht_params);
70845c9a7aSYuval Mintz 	rhl_for_each_entry_rcu(c, tmp, list, mnode)
71845c9a7aSYuval Mintz 		if (parent == -1 || parent == c->mfc_parent)
72845c9a7aSYuval Mintz 			return c;
73845c9a7aSYuval Mintz 
74845c9a7aSYuval Mintz 	return NULL;
75845c9a7aSYuval Mintz }
76845c9a7aSYuval Mintz EXPORT_SYMBOL(mr_mfc_find_parent);
77845c9a7aSYuval Mintz 
mr_mfc_find_any_parent(struct mr_table * mrt,int vifi)78845c9a7aSYuval Mintz void *mr_mfc_find_any_parent(struct mr_table *mrt, int vifi)
79845c9a7aSYuval Mintz {
80845c9a7aSYuval Mintz 	struct rhlist_head *tmp, *list;
81845c9a7aSYuval Mintz 	struct mr_mfc *c;
82845c9a7aSYuval Mintz 
83845c9a7aSYuval Mintz 	list = rhltable_lookup(&mrt->mfc_hash, mrt->ops.cmparg_any,
84845c9a7aSYuval Mintz 			       *mrt->ops.rht_params);
85845c9a7aSYuval Mintz 	rhl_for_each_entry_rcu(c, tmp, list, mnode)
86845c9a7aSYuval Mintz 		if (c->mfc_un.res.ttls[vifi] < 255)
87845c9a7aSYuval Mintz 			return c;
88845c9a7aSYuval Mintz 
89845c9a7aSYuval Mintz 	return NULL;
90845c9a7aSYuval Mintz }
91845c9a7aSYuval Mintz EXPORT_SYMBOL(mr_mfc_find_any_parent);
92845c9a7aSYuval Mintz 
mr_mfc_find_any(struct mr_table * mrt,int vifi,void * hasharg)93845c9a7aSYuval Mintz void *mr_mfc_find_any(struct mr_table *mrt, int vifi, void *hasharg)
94845c9a7aSYuval Mintz {
95845c9a7aSYuval Mintz 	struct rhlist_head *tmp, *list;
96845c9a7aSYuval Mintz 	struct mr_mfc *c, *proxy;
97845c9a7aSYuval Mintz 
98845c9a7aSYuval Mintz 	list = rhltable_lookup(&mrt->mfc_hash, hasharg, *mrt->ops.rht_params);
99845c9a7aSYuval Mintz 	rhl_for_each_entry_rcu(c, tmp, list, mnode) {
100845c9a7aSYuval Mintz 		if (c->mfc_un.res.ttls[vifi] < 255)
101845c9a7aSYuval Mintz 			return c;
102845c9a7aSYuval Mintz 
103845c9a7aSYuval Mintz 		/* It's ok if the vifi is part of the static tree */
104845c9a7aSYuval Mintz 		proxy = mr_mfc_find_any_parent(mrt, c->mfc_parent);
105845c9a7aSYuval Mintz 		if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
106845c9a7aSYuval Mintz 			return c;
107845c9a7aSYuval Mintz 	}
108845c9a7aSYuval Mintz 
109845c9a7aSYuval Mintz 	return mr_mfc_find_any_parent(mrt, vifi);
110845c9a7aSYuval Mintz }
111845c9a7aSYuval Mintz EXPORT_SYMBOL(mr_mfc_find_any);
112c8d61968SYuval Mintz 
113c8d61968SYuval Mintz #ifdef CONFIG_PROC_FS
mr_vif_seq_idx(struct net * net,struct mr_vif_iter * iter,loff_t pos)1143feda6b4SYuval Mintz void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter, loff_t pos)
1153feda6b4SYuval Mintz {
1163feda6b4SYuval Mintz 	struct mr_table *mrt = iter->mrt;
1173feda6b4SYuval Mintz 
1183feda6b4SYuval Mintz 	for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
1193feda6b4SYuval Mintz 		if (!VIF_EXISTS(mrt, iter->ct))
1203feda6b4SYuval Mintz 			continue;
1213feda6b4SYuval Mintz 		if (pos-- == 0)
1223feda6b4SYuval Mintz 			return &mrt->vif_table[iter->ct];
1233feda6b4SYuval Mintz 	}
1243feda6b4SYuval Mintz 	return NULL;
1253feda6b4SYuval Mintz }
1263feda6b4SYuval Mintz EXPORT_SYMBOL(mr_vif_seq_idx);
1273feda6b4SYuval Mintz 
mr_vif_seq_next(struct seq_file * seq,void * v,loff_t * pos)1283feda6b4SYuval Mintz void *mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
1293feda6b4SYuval Mintz {
1303feda6b4SYuval Mintz 	struct mr_vif_iter *iter = seq->private;
1313feda6b4SYuval Mintz 	struct net *net = seq_file_net(seq);
1323feda6b4SYuval Mintz 	struct mr_table *mrt = iter->mrt;
1333feda6b4SYuval Mintz 
1343feda6b4SYuval Mintz 	++*pos;
1353feda6b4SYuval Mintz 	if (v == SEQ_START_TOKEN)
1363feda6b4SYuval Mintz 		return mr_vif_seq_idx(net, iter, 0);
1373feda6b4SYuval Mintz 
1383feda6b4SYuval Mintz 	while (++iter->ct < mrt->maxvif) {
1393feda6b4SYuval Mintz 		if (!VIF_EXISTS(mrt, iter->ct))
1403feda6b4SYuval Mintz 			continue;
1413feda6b4SYuval Mintz 		return &mrt->vif_table[iter->ct];
1423feda6b4SYuval Mintz 	}
1433feda6b4SYuval Mintz 	return NULL;
1443feda6b4SYuval Mintz }
1453feda6b4SYuval Mintz EXPORT_SYMBOL(mr_vif_seq_next);
1463feda6b4SYuval Mintz 
mr_mfc_seq_idx(struct net * net,struct mr_mfc_iter * it,loff_t pos)147c8d61968SYuval Mintz void *mr_mfc_seq_idx(struct net *net,
148c8d61968SYuval Mintz 		     struct mr_mfc_iter *it, loff_t pos)
149c8d61968SYuval Mintz {
150c8d61968SYuval Mintz 	struct mr_table *mrt = it->mrt;
151c8d61968SYuval Mintz 	struct mr_mfc *mfc;
152c8d61968SYuval Mintz 
153c8d61968SYuval Mintz 	rcu_read_lock();
154c8d61968SYuval Mintz 	it->cache = &mrt->mfc_cache_list;
155c8d61968SYuval Mintz 	list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
156c8d61968SYuval Mintz 		if (pos-- == 0)
157c8d61968SYuval Mintz 			return mfc;
158c8d61968SYuval Mintz 	rcu_read_unlock();
159c8d61968SYuval Mintz 
160c8d61968SYuval Mintz 	spin_lock_bh(it->lock);
161c8d61968SYuval Mintz 	it->cache = &mrt->mfc_unres_queue;
162c8d61968SYuval Mintz 	list_for_each_entry(mfc, it->cache, list)
163c8d61968SYuval Mintz 		if (pos-- == 0)
164c8d61968SYuval Mintz 			return mfc;
165c8d61968SYuval Mintz 	spin_unlock_bh(it->lock);
166c8d61968SYuval Mintz 
167c8d61968SYuval Mintz 	it->cache = NULL;
168c8d61968SYuval Mintz 	return NULL;
169c8d61968SYuval Mintz }
170c8d61968SYuval Mintz EXPORT_SYMBOL(mr_mfc_seq_idx);
171c8d61968SYuval Mintz 
mr_mfc_seq_next(struct seq_file * seq,void * v,loff_t * pos)172c8d61968SYuval Mintz void *mr_mfc_seq_next(struct seq_file *seq, void *v,
173c8d61968SYuval Mintz 		      loff_t *pos)
174c8d61968SYuval Mintz {
175c8d61968SYuval Mintz 	struct mr_mfc_iter *it = seq->private;
176c8d61968SYuval Mintz 	struct net *net = seq_file_net(seq);
177c8d61968SYuval Mintz 	struct mr_table *mrt = it->mrt;
178c8d61968SYuval Mintz 	struct mr_mfc *c = v;
179c8d61968SYuval Mintz 
180c8d61968SYuval Mintz 	++*pos;
181c8d61968SYuval Mintz 
182c8d61968SYuval Mintz 	if (v == SEQ_START_TOKEN)
183c8d61968SYuval Mintz 		return mr_mfc_seq_idx(net, seq->private, 0);
184c8d61968SYuval Mintz 
185c8d61968SYuval Mintz 	if (c->list.next != it->cache)
186c8d61968SYuval Mintz 		return list_entry(c->list.next, struct mr_mfc, list);
187c8d61968SYuval Mintz 
188c8d61968SYuval Mintz 	if (it->cache == &mrt->mfc_unres_queue)
189c8d61968SYuval Mintz 		goto end_of_list;
190c8d61968SYuval Mintz 
191c8d61968SYuval Mintz 	/* exhausted cache_array, show unresolved */
192c8d61968SYuval Mintz 	rcu_read_unlock();
193c8d61968SYuval Mintz 	it->cache = &mrt->mfc_unres_queue;
194c8d61968SYuval Mintz 
195c8d61968SYuval Mintz 	spin_lock_bh(it->lock);
196c8d61968SYuval Mintz 	if (!list_empty(it->cache))
197c8d61968SYuval Mintz 		return list_first_entry(it->cache, struct mr_mfc, list);
198c8d61968SYuval Mintz 
199c8d61968SYuval Mintz end_of_list:
200c8d61968SYuval Mintz 	spin_unlock_bh(it->lock);
201c8d61968SYuval Mintz 	it->cache = NULL;
202c8d61968SYuval Mintz 
203c8d61968SYuval Mintz 	return NULL;
204c8d61968SYuval Mintz }
205c8d61968SYuval Mintz EXPORT_SYMBOL(mr_mfc_seq_next);
206c8d61968SYuval Mintz #endif
2077b0db857SYuval Mintz 
mr_fill_mroute(struct mr_table * mrt,struct sk_buff * skb,struct mr_mfc * c,struct rtmsg * rtm)2087b0db857SYuval Mintz int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2097b0db857SYuval Mintz 		   struct mr_mfc *c, struct rtmsg *rtm)
2107b0db857SYuval Mintz {
211ebc31979SEric Dumazet 	struct net_device *vif_dev;
2127b0db857SYuval Mintz 	struct rta_mfc_stats mfcs;
2137b0db857SYuval Mintz 	struct nlattr *mp_attr;
2147b0db857SYuval Mintz 	struct rtnexthop *nhp;
2157b0db857SYuval Mintz 	unsigned long lastuse;
2167b0db857SYuval Mintz 	int ct;
2177b0db857SYuval Mintz 
2187b0db857SYuval Mintz 	/* If cache is unresolved, don't try to parse IIF and OIF */
2197b0db857SYuval Mintz 	if (c->mfc_parent >= MAXVIFS) {
2207b0db857SYuval Mintz 		rtm->rtm_flags |= RTNH_F_UNRESOLVED;
2217b0db857SYuval Mintz 		return -ENOENT;
2227b0db857SYuval Mintz 	}
2237b0db857SYuval Mintz 
224ebc31979SEric Dumazet 	rcu_read_lock();
225ebc31979SEric Dumazet 	vif_dev = rcu_dereference(mrt->vif_table[c->mfc_parent].dev);
226ebc31979SEric Dumazet 	if (vif_dev && nla_put_u32(skb, RTA_IIF, vif_dev->ifindex) < 0) {
227ebc31979SEric Dumazet 		rcu_read_unlock();
2287b0db857SYuval Mintz 		return -EMSGSIZE;
229ebc31979SEric Dumazet 	}
230ebc31979SEric Dumazet 	rcu_read_unlock();
2317b0db857SYuval Mintz 
2327b0db857SYuval Mintz 	if (c->mfc_flags & MFC_OFFLOAD)
2337b0db857SYuval Mintz 		rtm->rtm_flags |= RTNH_F_OFFLOAD;
2347b0db857SYuval Mintz 
235ae0be8deSMichal Kubecek 	mp_attr = nla_nest_start_noflag(skb, RTA_MULTIPATH);
2367b0db857SYuval Mintz 	if (!mp_attr)
2377b0db857SYuval Mintz 		return -EMSGSIZE;
2387b0db857SYuval Mintz 
239ebc31979SEric Dumazet 	rcu_read_lock();
2407b0db857SYuval Mintz 	for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
241ebc31979SEric Dumazet 		struct vif_device *vif = &mrt->vif_table[ct];
242ebc31979SEric Dumazet 
243ebc31979SEric Dumazet 		vif_dev = rcu_dereference(vif->dev);
244ebc31979SEric Dumazet 		if (vif_dev && c->mfc_un.res.ttls[ct] < 255) {
2457b0db857SYuval Mintz 
2467b0db857SYuval Mintz 			nhp = nla_reserve_nohdr(skb, sizeof(*nhp));
2477b0db857SYuval Mintz 			if (!nhp) {
248ebc31979SEric Dumazet 				rcu_read_unlock();
2497b0db857SYuval Mintz 				nla_nest_cancel(skb, mp_attr);
2507b0db857SYuval Mintz 				return -EMSGSIZE;
2517b0db857SYuval Mintz 			}
2527b0db857SYuval Mintz 
2537b0db857SYuval Mintz 			nhp->rtnh_flags = 0;
2547b0db857SYuval Mintz 			nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
255ebc31979SEric Dumazet 			nhp->rtnh_ifindex = vif_dev->ifindex;
2567b0db857SYuval Mintz 			nhp->rtnh_len = sizeof(*nhp);
2577b0db857SYuval Mintz 		}
2587b0db857SYuval Mintz 	}
259ebc31979SEric Dumazet 	rcu_read_unlock();
2607b0db857SYuval Mintz 
2617b0db857SYuval Mintz 	nla_nest_end(skb, mp_attr);
2627b0db857SYuval Mintz 
2637b0db857SYuval Mintz 	lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2647b0db857SYuval Mintz 	lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2657b0db857SYuval Mintz 
2667b0db857SYuval Mintz 	mfcs.mfcs_packets = c->mfc_un.res.pkt;
2677b0db857SYuval Mintz 	mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2687b0db857SYuval Mintz 	mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2697b0db857SYuval Mintz 	if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
2707b0db857SYuval Mintz 	    nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
2717b0db857SYuval Mintz 			      RTA_PAD))
2727b0db857SYuval Mintz 		return -EMSGSIZE;
2737b0db857SYuval Mintz 
2747b0db857SYuval Mintz 	rtm->rtm_type = RTN_MULTICAST;
2757b0db857SYuval Mintz 	return 1;
2767b0db857SYuval Mintz }
2777b0db857SYuval Mintz EXPORT_SYMBOL(mr_fill_mroute);
2787b0db857SYuval Mintz 
mr_mfc_uses_dev(const struct mr_table * mrt,const struct mr_mfc * c,const struct net_device * dev)279cb167893SDavid Ahern static bool mr_mfc_uses_dev(const struct mr_table *mrt,
280cb167893SDavid Ahern 			    const struct mr_mfc *c,
281cb167893SDavid Ahern 			    const struct net_device *dev)
282cb167893SDavid Ahern {
283cb167893SDavid Ahern 	int ct;
284cb167893SDavid Ahern 
285cb167893SDavid Ahern 	for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
286ebc31979SEric Dumazet 		const struct net_device *vif_dev;
287cb167893SDavid Ahern 		const struct vif_device *vif;
288cb167893SDavid Ahern 
289cb167893SDavid Ahern 		vif = &mrt->vif_table[ct];
290ebc31979SEric Dumazet 		vif_dev = rcu_access_pointer(vif->dev);
291ebc31979SEric Dumazet 		if (vif_dev && c->mfc_un.res.ttls[ct] < 255 &&
292ebc31979SEric Dumazet 		    vif_dev == dev)
293cb167893SDavid Ahern 			return true;
294cb167893SDavid Ahern 	}
295cb167893SDavid Ahern 	return false;
296cb167893SDavid Ahern }
297cb167893SDavid Ahern 
mr_table_dump(struct mr_table * mrt,struct sk_buff * skb,struct netlink_callback * cb,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)298e1cedae1SDavid Ahern int mr_table_dump(struct mr_table *mrt, struct sk_buff *skb,
299e1cedae1SDavid Ahern 		  struct netlink_callback *cb,
300e1cedae1SDavid Ahern 		  int (*fill)(struct mr_table *mrt, struct sk_buff *skb,
3017b0db857SYuval Mintz 			      u32 portid, u32 seq, struct mr_mfc *c,
3027b0db857SYuval Mintz 			      int cmd, int flags),
303cb167893SDavid Ahern 		  spinlock_t *lock, struct fib_dump_filter *filter)
3047b0db857SYuval Mintz {
305e1cedae1SDavid Ahern 	unsigned int e = 0, s_e = cb->args[1];
306e1cedae1SDavid Ahern 	unsigned int flags = NLM_F_MULTI;
3077b0db857SYuval Mintz 	struct mr_mfc *mfc;
308e1cedae1SDavid Ahern 	int err;
3097b0db857SYuval Mintz 
310cb167893SDavid Ahern 	if (filter->filter_set)
311cb167893SDavid Ahern 		flags |= NLM_F_DUMP_FILTERED;
312cb167893SDavid Ahern 
3137b0db857SYuval Mintz 	list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
3147b0db857SYuval Mintz 		if (e < s_e)
3157b0db857SYuval Mintz 			goto next_entry;
316cb167893SDavid Ahern 		if (filter->dev &&
317cb167893SDavid Ahern 		    !mr_mfc_uses_dev(mrt, mfc, filter->dev))
318cb167893SDavid Ahern 			goto next_entry;
319e1cedae1SDavid Ahern 
320e1cedae1SDavid Ahern 		err = fill(mrt, skb, NETLINK_CB(cb->skb).portid,
321e1cedae1SDavid Ahern 			   cb->nlh->nlmsg_seq, mfc, RTM_NEWROUTE, flags);
322e1cedae1SDavid Ahern 		if (err < 0)
323e1cedae1SDavid Ahern 			goto out;
3247b0db857SYuval Mintz next_entry:
3257b0db857SYuval Mintz 		e++;
3267b0db857SYuval Mintz 	}
3277b0db857SYuval Mintz 
3287b0db857SYuval Mintz 	spin_lock_bh(lock);
3297b0db857SYuval Mintz 	list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
3307b0db857SYuval Mintz 		if (e < s_e)
3317b0db857SYuval Mintz 			goto next_entry2;
332cb167893SDavid Ahern 		if (filter->dev &&
333cb167893SDavid Ahern 		    !mr_mfc_uses_dev(mrt, mfc, filter->dev))
334cb167893SDavid Ahern 			goto next_entry2;
335e1cedae1SDavid Ahern 
336e1cedae1SDavid Ahern 		err = fill(mrt, skb, NETLINK_CB(cb->skb).portid,
337e1cedae1SDavid Ahern 			   cb->nlh->nlmsg_seq, mfc, RTM_NEWROUTE, flags);
338e1cedae1SDavid Ahern 		if (err < 0) {
3397b0db857SYuval Mintz 			spin_unlock_bh(lock);
340e1cedae1SDavid Ahern 			goto out;
3417b0db857SYuval Mintz 		}
3427b0db857SYuval Mintz next_entry2:
3437b0db857SYuval Mintz 		e++;
3447b0db857SYuval Mintz 	}
3457b0db857SYuval Mintz 	spin_unlock_bh(lock);
346e1cedae1SDavid Ahern 	err = 0;
347e1cedae1SDavid Ahern out:
348e1cedae1SDavid Ahern 	cb->args[1] = e;
349e1cedae1SDavid Ahern 	return err;
350e1cedae1SDavid Ahern }
351cb167893SDavid Ahern EXPORT_SYMBOL(mr_table_dump);
352e1cedae1SDavid Ahern 
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)353e1cedae1SDavid Ahern int mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
354e1cedae1SDavid Ahern 		     struct mr_table *(*iter)(struct net *net,
355e1cedae1SDavid Ahern 					      struct mr_table *mrt),
356e1cedae1SDavid Ahern 		     int (*fill)(struct mr_table *mrt,
357e1cedae1SDavid Ahern 				 struct sk_buff *skb,
358e1cedae1SDavid Ahern 				 u32 portid, u32 seq, struct mr_mfc *c,
359e1cedae1SDavid Ahern 				 int cmd, int flags),
360cb167893SDavid Ahern 		     spinlock_t *lock, struct fib_dump_filter *filter)
361e1cedae1SDavid Ahern {
362e1cedae1SDavid Ahern 	unsigned int t = 0, s_t = cb->args[0];
363e1cedae1SDavid Ahern 	struct net *net = sock_net(skb->sk);
364e1cedae1SDavid Ahern 	struct mr_table *mrt;
365e1cedae1SDavid Ahern 	int err;
366e1cedae1SDavid Ahern 
367cb167893SDavid Ahern 	/* multicast does not track protocol or have route type other
368cb167893SDavid Ahern 	 * than RTN_MULTICAST
369cb167893SDavid Ahern 	 */
370cb167893SDavid Ahern 	if (filter->filter_set) {
371cb167893SDavid Ahern 		if (filter->protocol || filter->flags ||
372cb167893SDavid Ahern 		    (filter->rt_type && filter->rt_type != RTN_MULTICAST))
373cb167893SDavid Ahern 			return skb->len;
374cb167893SDavid Ahern 	}
375cb167893SDavid Ahern 
376e1cedae1SDavid Ahern 	rcu_read_lock();
377e1cedae1SDavid Ahern 	for (mrt = iter(net, NULL); mrt; mrt = iter(net, mrt)) {
378e1cedae1SDavid Ahern 		if (t < s_t)
379e1cedae1SDavid Ahern 			goto next_table;
380e1cedae1SDavid Ahern 
381cb167893SDavid Ahern 		err = mr_table_dump(mrt, skb, cb, fill, lock, filter);
382e1cedae1SDavid Ahern 		if (err < 0)
383e1cedae1SDavid Ahern 			break;
3847fcd1e03SDavid Ahern 		cb->args[1] = 0;
3857b0db857SYuval Mintz next_table:
3867b0db857SYuval Mintz 		t++;
3877b0db857SYuval Mintz 	}
3887b0db857SYuval Mintz 	rcu_read_unlock();
3897b0db857SYuval Mintz 
3907b0db857SYuval Mintz 	cb->args[0] = t;
3917b0db857SYuval Mintz 
3927b0db857SYuval Mintz 	return skb->len;
3937b0db857SYuval Mintz }
3947b0db857SYuval Mintz EXPORT_SYMBOL(mr_rtm_dumproute);
395cdc9f944SYuval Mintz 
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)396cdc9f944SYuval Mintz int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family,
397cdc9f944SYuval Mintz 	    int (*rules_dump)(struct net *net,
398b7a59557SJiri Pirko 			      struct notifier_block *nb,
399b7a59557SJiri Pirko 			      struct netlink_ext_ack *extack),
400cdc9f944SYuval Mintz 	    struct mr_table *(*mr_iter)(struct net *net,
401cdc9f944SYuval Mintz 					struct mr_table *mrt),
402b7a59557SJiri Pirko 	    struct netlink_ext_ack *extack)
403cdc9f944SYuval Mintz {
404cdc9f944SYuval Mintz 	struct mr_table *mrt;
405cdc9f944SYuval Mintz 	int err;
406cdc9f944SYuval Mintz 
407b7a59557SJiri Pirko 	err = rules_dump(net, nb, extack);
408cdc9f944SYuval Mintz 	if (err)
409cdc9f944SYuval Mintz 		return err;
410cdc9f944SYuval Mintz 
411cdc9f944SYuval Mintz 	for (mrt = mr_iter(net, NULL); mrt; mrt = mr_iter(net, mrt)) {
412cdc9f944SYuval Mintz 		struct vif_device *v = &mrt->vif_table[0];
413ebc31979SEric Dumazet 		struct net_device *vif_dev;
414cdc9f944SYuval Mintz 		struct mr_mfc *mfc;
415cdc9f944SYuval Mintz 		int vifi;
416cdc9f944SYuval Mintz 
417cdc9f944SYuval Mintz 		/* Notifiy on table VIF entries */
418*194366b2SEric Dumazet 		rcu_read_lock();
419cdc9f944SYuval Mintz 		for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
420*194366b2SEric Dumazet 			vif_dev = rcu_dereference(v->dev);
421ebc31979SEric Dumazet 			if (!vif_dev)
422cdc9f944SYuval Mintz 				continue;
423cdc9f944SYuval Mintz 
42455c894f7SJiri Pirko 			err = mr_call_vif_notifier(nb, family,
425ebc31979SEric Dumazet 						   FIB_EVENT_VIF_ADD, v,
426ebc31979SEric Dumazet 						   vif_dev, vifi,
427ebc31979SEric Dumazet 						   mrt->id, extack);
42855c894f7SJiri Pirko 			if (err)
42955c894f7SJiri Pirko 				break;
430cdc9f944SYuval Mintz 		}
431*194366b2SEric Dumazet 		rcu_read_unlock();
432cdc9f944SYuval Mintz 
43355c894f7SJiri Pirko 		if (err)
43455c894f7SJiri Pirko 			return err;
43555c894f7SJiri Pirko 
436cdc9f944SYuval Mintz 		/* Notify on table MFC entries */
43755c894f7SJiri Pirko 		list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
43855c894f7SJiri Pirko 			err = mr_call_mfc_notifier(nb, family,
439cdc9f944SYuval Mintz 						   FIB_EVENT_ENTRY_ADD,
440b7a59557SJiri Pirko 						   mfc, mrt->id, extack);
44155c894f7SJiri Pirko 			if (err)
44255c894f7SJiri Pirko 				return err;
44355c894f7SJiri Pirko 		}
444cdc9f944SYuval Mintz 	}
445cdc9f944SYuval Mintz 
446cdc9f944SYuval Mintz 	return 0;
447cdc9f944SYuval Mintz }
448cdc9f944SYuval Mintz EXPORT_SYMBOL(mr_dump);
449