1 /* Linux multicast routing support
2 * Common logic shared by IPv4 [ipmr] and IPv6 [ip6mr] implementation
3 */
4
5 #include <linux/rhashtable.h>
6 #include <linux/mroute_base.h>
7
8 /* 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)9 void vif_device_init(struct vif_device *v,
10 struct net_device *dev,
11 unsigned long rate_limit,
12 unsigned char threshold,
13 unsigned short flags,
14 unsigned short get_iflink_mask)
15 {
16 RCU_INIT_POINTER(v->dev, NULL);
17 v->bytes_in = 0;
18 v->bytes_out = 0;
19 v->pkt_in = 0;
20 v->pkt_out = 0;
21 v->rate_limit = rate_limit;
22 v->flags = flags;
23 v->threshold = threshold;
24 if (v->flags & get_iflink_mask)
25 v->link = dev_get_iflink(dev);
26 else
27 v->link = dev->ifindex;
28 }
29 EXPORT_SYMBOL(vif_device_init);
30
31 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))32 mr_table_alloc(struct net *net, u32 id,
33 struct mr_table_ops *ops,
34 void (*expire_func)(struct timer_list *t),
35 void (*table_set)(struct mr_table *mrt,
36 struct net *net))
37 {
38 struct mr_table *mrt;
39 int err;
40
41 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
42 if (!mrt)
43 return ERR_PTR(-ENOMEM);
44 mrt->id = id;
45 write_pnet(&mrt->net, net);
46
47 mrt->ops = *ops;
48 err = rhltable_init(&mrt->mfc_hash, mrt->ops.rht_params);
49 if (err) {
50 kfree(mrt);
51 return ERR_PTR(err);
52 }
53 INIT_LIST_HEAD(&mrt->mfc_cache_list);
54 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
55
56 timer_setup(&mrt->ipmr_expire_timer, expire_func, 0);
57
58 mrt->mroute_reg_vif_num = -1;
59 table_set(mrt, net);
60 return mrt;
61 }
62 EXPORT_SYMBOL(mr_table_alloc);
63
mr_mfc_find_parent(struct mr_table * mrt,void * hasharg,int parent)64 void *mr_mfc_find_parent(struct mr_table *mrt, void *hasharg, int parent)
65 {
66 struct rhlist_head *tmp, *list;
67 struct mr_mfc *c;
68
69 list = rhltable_lookup(&mrt->mfc_hash, hasharg, *mrt->ops.rht_params);
70 rhl_for_each_entry_rcu(c, tmp, list, mnode)
71 if (parent == -1 || parent == c->mfc_parent)
72 return c;
73
74 return NULL;
75 }
76 EXPORT_SYMBOL(mr_mfc_find_parent);
77
mr_mfc_find_any_parent(struct mr_table * mrt,int vifi)78 void *mr_mfc_find_any_parent(struct mr_table *mrt, int vifi)
79 {
80 struct rhlist_head *tmp, *list;
81 struct mr_mfc *c;
82
83 list = rhltable_lookup(&mrt->mfc_hash, mrt->ops.cmparg_any,
84 *mrt->ops.rht_params);
85 rhl_for_each_entry_rcu(c, tmp, list, mnode)
86 if (c->mfc_un.res.ttls[vifi] < 255)
87 return c;
88
89 return NULL;
90 }
91 EXPORT_SYMBOL(mr_mfc_find_any_parent);
92
mr_mfc_find_any(struct mr_table * mrt,int vifi,void * hasharg)93 void *mr_mfc_find_any(struct mr_table *mrt, int vifi, void *hasharg)
94 {
95 struct rhlist_head *tmp, *list;
96 struct mr_mfc *c, *proxy;
97
98 list = rhltable_lookup(&mrt->mfc_hash, hasharg, *mrt->ops.rht_params);
99 rhl_for_each_entry_rcu(c, tmp, list, mnode) {
100 if (c->mfc_un.res.ttls[vifi] < 255)
101 return c;
102
103 /* It's ok if the vifi is part of the static tree */
104 proxy = mr_mfc_find_any_parent(mrt, c->mfc_parent);
105 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
106 return c;
107 }
108
109 return mr_mfc_find_any_parent(mrt, vifi);
110 }
111 EXPORT_SYMBOL(mr_mfc_find_any);
112
113 #ifdef CONFIG_PROC_FS
mr_vif_seq_idx(struct net * net,struct mr_vif_iter * iter,loff_t pos)114 void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter, loff_t pos)
115 {
116 struct mr_table *mrt = iter->mrt;
117
118 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
119 if (!VIF_EXISTS(mrt, iter->ct))
120 continue;
121 if (pos-- == 0)
122 return &mrt->vif_table[iter->ct];
123 }
124 return NULL;
125 }
126 EXPORT_SYMBOL(mr_vif_seq_idx);
127
mr_vif_seq_next(struct seq_file * seq,void * v,loff_t * pos)128 void *mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
129 {
130 struct mr_vif_iter *iter = seq->private;
131 struct net *net = seq_file_net(seq);
132 struct mr_table *mrt = iter->mrt;
133
134 ++*pos;
135 if (v == SEQ_START_TOKEN)
136 return mr_vif_seq_idx(net, iter, 0);
137
138 while (++iter->ct < mrt->maxvif) {
139 if (!VIF_EXISTS(mrt, iter->ct))
140 continue;
141 return &mrt->vif_table[iter->ct];
142 }
143 return NULL;
144 }
145 EXPORT_SYMBOL(mr_vif_seq_next);
146
mr_mfc_seq_idx(struct net * net,struct mr_mfc_iter * it,loff_t pos)147 void *mr_mfc_seq_idx(struct net *net,
148 struct mr_mfc_iter *it, loff_t pos)
149 {
150 struct mr_table *mrt = it->mrt;
151 struct mr_mfc *mfc;
152
153 rcu_read_lock();
154 it->cache = &mrt->mfc_cache_list;
155 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
156 if (pos-- == 0)
157 return mfc;
158 rcu_read_unlock();
159
160 spin_lock_bh(it->lock);
161 it->cache = &mrt->mfc_unres_queue;
162 list_for_each_entry(mfc, it->cache, list)
163 if (pos-- == 0)
164 return mfc;
165 spin_unlock_bh(it->lock);
166
167 it->cache = NULL;
168 return NULL;
169 }
170 EXPORT_SYMBOL(mr_mfc_seq_idx);
171
mr_mfc_seq_next(struct seq_file * seq,void * v,loff_t * pos)172 void *mr_mfc_seq_next(struct seq_file *seq, void *v,
173 loff_t *pos)
174 {
175 struct mr_mfc_iter *it = seq->private;
176 struct net *net = seq_file_net(seq);
177 struct mr_table *mrt = it->mrt;
178 struct mr_mfc *c = v;
179
180 ++*pos;
181
182 if (v == SEQ_START_TOKEN)
183 return mr_mfc_seq_idx(net, seq->private, 0);
184
185 if (c->list.next != it->cache)
186 return list_entry(c->list.next, struct mr_mfc, list);
187
188 if (it->cache == &mrt->mfc_unres_queue)
189 goto end_of_list;
190
191 /* exhausted cache_array, show unresolved */
192 rcu_read_unlock();
193 it->cache = &mrt->mfc_unres_queue;
194
195 spin_lock_bh(it->lock);
196 if (!list_empty(it->cache))
197 return list_first_entry(it->cache, struct mr_mfc, list);
198
199 end_of_list:
200 spin_unlock_bh(it->lock);
201 it->cache = NULL;
202
203 return NULL;
204 }
205 EXPORT_SYMBOL(mr_mfc_seq_next);
206 #endif
207
mr_fill_mroute(struct mr_table * mrt,struct sk_buff * skb,struct mr_mfc * c,struct rtmsg * rtm)208 int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
209 struct mr_mfc *c, struct rtmsg *rtm)
210 {
211 struct net_device *vif_dev;
212 struct rta_mfc_stats mfcs;
213 struct nlattr *mp_attr;
214 struct rtnexthop *nhp;
215 unsigned long lastuse;
216 int ct;
217
218 /* If cache is unresolved, don't try to parse IIF and OIF */
219 if (c->mfc_parent >= MAXVIFS) {
220 rtm->rtm_flags |= RTNH_F_UNRESOLVED;
221 return -ENOENT;
222 }
223
224 rcu_read_lock();
225 vif_dev = rcu_dereference(mrt->vif_table[c->mfc_parent].dev);
226 if (vif_dev && nla_put_u32(skb, RTA_IIF, vif_dev->ifindex) < 0) {
227 rcu_read_unlock();
228 return -EMSGSIZE;
229 }
230 rcu_read_unlock();
231
232 if (c->mfc_flags & MFC_OFFLOAD)
233 rtm->rtm_flags |= RTNH_F_OFFLOAD;
234
235 mp_attr = nla_nest_start_noflag(skb, RTA_MULTIPATH);
236 if (!mp_attr)
237 return -EMSGSIZE;
238
239 rcu_read_lock();
240 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
241 struct vif_device *vif = &mrt->vif_table[ct];
242
243 vif_dev = rcu_dereference(vif->dev);
244 if (vif_dev && c->mfc_un.res.ttls[ct] < 255) {
245
246 nhp = nla_reserve_nohdr(skb, sizeof(*nhp));
247 if (!nhp) {
248 rcu_read_unlock();
249 nla_nest_cancel(skb, mp_attr);
250 return -EMSGSIZE;
251 }
252
253 nhp->rtnh_flags = 0;
254 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
255 nhp->rtnh_ifindex = vif_dev->ifindex;
256 nhp->rtnh_len = sizeof(*nhp);
257 }
258 }
259 rcu_read_unlock();
260
261 nla_nest_end(skb, mp_attr);
262
263 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
264 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
265
266 mfcs.mfcs_packets = c->mfc_un.res.pkt;
267 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
268 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
269 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
270 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
271 RTA_PAD))
272 return -EMSGSIZE;
273
274 rtm->rtm_type = RTN_MULTICAST;
275 return 1;
276 }
277 EXPORT_SYMBOL(mr_fill_mroute);
278
mr_mfc_uses_dev(const struct mr_table * mrt,const struct mr_mfc * c,const struct net_device * dev)279 static bool mr_mfc_uses_dev(const struct mr_table *mrt,
280 const struct mr_mfc *c,
281 const struct net_device *dev)
282 {
283 int ct;
284
285 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
286 const struct net_device *vif_dev;
287 const struct vif_device *vif;
288
289 vif = &mrt->vif_table[ct];
290 vif_dev = rcu_access_pointer(vif->dev);
291 if (vif_dev && c->mfc_un.res.ttls[ct] < 255 &&
292 vif_dev == dev)
293 return true;
294 }
295 return false;
296 }
297
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)298 int mr_table_dump(struct mr_table *mrt, struct sk_buff *skb,
299 struct netlink_callback *cb,
300 int (*fill)(struct mr_table *mrt, struct sk_buff *skb,
301 u32 portid, u32 seq, struct mr_mfc *c,
302 int cmd, int flags),
303 spinlock_t *lock, struct fib_dump_filter *filter)
304 {
305 unsigned int e = 0, s_e = cb->args[1];
306 unsigned int flags = NLM_F_MULTI;
307 struct mr_mfc *mfc;
308 int err;
309
310 if (filter->filter_set)
311 flags |= NLM_F_DUMP_FILTERED;
312
313 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list,
314 lockdep_rtnl_is_held()) {
315 if (e < s_e)
316 goto next_entry;
317 if (filter->dev &&
318 !mr_mfc_uses_dev(mrt, mfc, filter->dev))
319 goto next_entry;
320
321 err = fill(mrt, skb, NETLINK_CB(cb->skb).portid,
322 cb->nlh->nlmsg_seq, mfc, RTM_NEWROUTE, flags);
323 if (err < 0)
324 goto out;
325 next_entry:
326 e++;
327 }
328
329 spin_lock_bh(lock);
330 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
331 if (e < s_e)
332 goto next_entry2;
333 if (filter->dev &&
334 !mr_mfc_uses_dev(mrt, mfc, filter->dev))
335 goto next_entry2;
336
337 err = fill(mrt, skb, NETLINK_CB(cb->skb).portid,
338 cb->nlh->nlmsg_seq, mfc, RTM_NEWROUTE, flags);
339 if (err < 0) {
340 spin_unlock_bh(lock);
341 goto out;
342 }
343 next_entry2:
344 e++;
345 }
346 spin_unlock_bh(lock);
347 err = 0;
348 out:
349 cb->args[1] = e;
350 return err;
351 }
352 EXPORT_SYMBOL(mr_table_dump);
353
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 int 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 unsigned int t = 0, s_t = cb->args[0];
364 struct net *net = sock_net(skb->sk);
365 struct mr_table *mrt;
366 int err;
367
368 /* multicast does not track protocol or have route type other
369 * than RTN_MULTICAST
370 */
371 if (filter->filter_set) {
372 if (filter->protocol || filter->flags ||
373 (filter->rt_type && filter->rt_type != RTN_MULTICAST))
374 return skb->len;
375 }
376
377 rcu_read_lock();
378 for (mrt = iter(net, NULL); mrt; mrt = iter(net, mrt)) {
379 if (t < s_t)
380 goto next_table;
381
382 err = mr_table_dump(mrt, skb, cb, fill, lock, filter);
383 if (err < 0)
384 break;
385 cb->args[1] = 0;
386 next_table:
387 t++;
388 }
389 rcu_read_unlock();
390
391 cb->args[0] = t;
392
393 return skb->len;
394 }
395 EXPORT_SYMBOL(mr_rtm_dumproute);
396
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)397 int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family,
398 int (*rules_dump)(struct net *net,
399 struct notifier_block *nb,
400 struct netlink_ext_ack *extack),
401 struct mr_table *(*mr_iter)(struct net *net,
402 struct mr_table *mrt),
403 struct netlink_ext_ack *extack)
404 {
405 struct mr_table *mrt;
406 int err;
407
408 err = rules_dump(net, nb, extack);
409 if (err)
410 return err;
411
412 for (mrt = mr_iter(net, NULL); mrt; mrt = mr_iter(net, mrt)) {
413 struct vif_device *v = &mrt->vif_table[0];
414 struct net_device *vif_dev;
415 struct mr_mfc *mfc;
416 int vifi;
417
418 /* Notifiy on table VIF entries */
419 rcu_read_lock();
420 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
421 vif_dev = rcu_dereference(v->dev);
422 if (!vif_dev)
423 continue;
424
425 err = mr_call_vif_notifier(nb, family,
426 FIB_EVENT_VIF_ADD, v,
427 vif_dev, vifi,
428 mrt->id, extack);
429 if (err)
430 break;
431 }
432 rcu_read_unlock();
433
434 if (err)
435 return err;
436
437 /* Notify on table MFC entries */
438 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
439 err = mr_call_mfc_notifier(nb, family,
440 FIB_EVENT_ENTRY_ADD,
441 mfc, mrt->id, extack);
442 if (err)
443 return err;
444 }
445 }
446
447 return 0;
448 }
449 EXPORT_SYMBOL(mr_dump);
450