Yuval Mintz | 6853f21 | 2018-02-28 23:29:29 +0200 | [diff] [blame] | 1 | /* Linux multicast routing support |
| 2 | * Common logic shared by IPv4 [ipmr] and IPv6 [ip6mr] implementation |
| 3 | */ |
| 4 | |
NeilBrown | 0eb71a9 | 2018-06-18 12:52:50 +1000 | [diff] [blame] | 5 | #include <linux/rhashtable.h> |
Yuval Mintz | 6853f21 | 2018-02-28 23:29:29 +0200 | [diff] [blame] | 6 | #include <linux/mroute_base.h> |
| 7 | |
| 8 | /* Sets everything common except 'dev', since that is done under locking */ |
| 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 | 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); |
Yuval Mintz | 0bbbf0e | 2018-02-28 23:29:33 +0200 | [diff] [blame] | 30 | |
| 31 | struct mr_table * |
| 32 | mr_table_alloc(struct net *net, u32 id, |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 33 | struct mr_table_ops *ops, |
Yuval Mintz | 0bbbf0e | 2018-02-28 23:29:33 +0200 | [diff] [blame] | 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; |
Sabrina Dubroca | e783bb0 | 2018-06-05 15:02:00 +0200 | [diff] [blame] | 39 | int err; |
Yuval Mintz | 0bbbf0e | 2018-02-28 23:29:33 +0200 | [diff] [blame] | 40 | |
| 41 | mrt = kzalloc(sizeof(*mrt), GFP_KERNEL); |
| 42 | if (!mrt) |
Sabrina Dubroca | e783bb0 | 2018-06-05 15:02:00 +0200 | [diff] [blame] | 43 | return ERR_PTR(-ENOMEM); |
Yuval Mintz | 0bbbf0e | 2018-02-28 23:29:33 +0200 | [diff] [blame] | 44 | mrt->id = id; |
| 45 | write_pnet(&mrt->net, net); |
| 46 | |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 47 | mrt->ops = *ops; |
Sabrina Dubroca | e783bb0 | 2018-06-05 15:02:00 +0200 | [diff] [blame] | 48 | err = rhltable_init(&mrt->mfc_hash, mrt->ops.rht_params); |
| 49 | if (err) { |
Eric Dumazet | 66fb332 | 2018-05-21 10:51:53 -0700 | [diff] [blame] | 50 | kfree(mrt); |
Sabrina Dubroca | e783bb0 | 2018-06-05 15:02:00 +0200 | [diff] [blame] | 51 | return ERR_PTR(err); |
Eric Dumazet | 66fb332 | 2018-05-21 10:51:53 -0700 | [diff] [blame] | 52 | } |
Yuval Mintz | 0bbbf0e | 2018-02-28 23:29:33 +0200 | [diff] [blame] | 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); |
Yuval Mintz | 845c9a7 | 2018-02-28 23:29:35 +0200 | [diff] [blame] | 63 | |
| 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 | |
| 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 | |
| 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); |
Yuval Mintz | c8d6196 | 2018-02-28 23:29:36 +0200 | [diff] [blame] | 112 | |
| 113 | #ifdef CONFIG_PROC_FS |
Yuval Mintz | 3feda6b | 2018-02-28 23:29:37 +0200 | [diff] [blame] | 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 | |
| 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 | |
Yuval Mintz | c8d6196 | 2018-02-28 23:29:36 +0200 | [diff] [blame] | 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 | |
| 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 |
Yuval Mintz | 7b0db85 | 2018-02-28 23:29:39 +0200 | [diff] [blame] | 207 | |
| 208 | int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, |
| 209 | struct mr_mfc *c, struct rtmsg *rtm) |
| 210 | { |
| 211 | struct rta_mfc_stats mfcs; |
| 212 | struct nlattr *mp_attr; |
| 213 | struct rtnexthop *nhp; |
| 214 | unsigned long lastuse; |
| 215 | int ct; |
| 216 | |
| 217 | /* If cache is unresolved, don't try to parse IIF and OIF */ |
| 218 | if (c->mfc_parent >= MAXVIFS) { |
| 219 | rtm->rtm_flags |= RTNH_F_UNRESOLVED; |
| 220 | return -ENOENT; |
| 221 | } |
| 222 | |
| 223 | if (VIF_EXISTS(mrt, c->mfc_parent) && |
| 224 | nla_put_u32(skb, RTA_IIF, |
| 225 | mrt->vif_table[c->mfc_parent].dev->ifindex) < 0) |
| 226 | return -EMSGSIZE; |
| 227 | |
| 228 | if (c->mfc_flags & MFC_OFFLOAD) |
| 229 | rtm->rtm_flags |= RTNH_F_OFFLOAD; |
| 230 | |
| 231 | mp_attr = nla_nest_start(skb, RTA_MULTIPATH); |
| 232 | if (!mp_attr) |
| 233 | return -EMSGSIZE; |
| 234 | |
| 235 | for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) { |
| 236 | if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) { |
| 237 | struct vif_device *vif; |
| 238 | |
| 239 | nhp = nla_reserve_nohdr(skb, sizeof(*nhp)); |
| 240 | if (!nhp) { |
| 241 | nla_nest_cancel(skb, mp_attr); |
| 242 | return -EMSGSIZE; |
| 243 | } |
| 244 | |
| 245 | nhp->rtnh_flags = 0; |
| 246 | nhp->rtnh_hops = c->mfc_un.res.ttls[ct]; |
| 247 | vif = &mrt->vif_table[ct]; |
| 248 | nhp->rtnh_ifindex = vif->dev->ifindex; |
| 249 | nhp->rtnh_len = sizeof(*nhp); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | nla_nest_end(skb, mp_attr); |
| 254 | |
| 255 | lastuse = READ_ONCE(c->mfc_un.res.lastuse); |
| 256 | lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0; |
| 257 | |
| 258 | mfcs.mfcs_packets = c->mfc_un.res.pkt; |
| 259 | mfcs.mfcs_bytes = c->mfc_un.res.bytes; |
| 260 | mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if; |
| 261 | if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) || |
| 262 | nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse), |
| 263 | RTA_PAD)) |
| 264 | return -EMSGSIZE; |
| 265 | |
| 266 | rtm->rtm_type = RTN_MULTICAST; |
| 267 | return 1; |
| 268 | } |
| 269 | EXPORT_SYMBOL(mr_fill_mroute); |
| 270 | |
| 271 | int mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb, |
| 272 | struct mr_table *(*iter)(struct net *net, |
| 273 | struct mr_table *mrt), |
| 274 | int (*fill)(struct mr_table *mrt, |
| 275 | struct sk_buff *skb, |
| 276 | u32 portid, u32 seq, struct mr_mfc *c, |
| 277 | int cmd, int flags), |
| 278 | spinlock_t *lock) |
| 279 | { |
| 280 | unsigned int t = 0, e = 0, s_t = cb->args[0], s_e = cb->args[1]; |
| 281 | struct net *net = sock_net(skb->sk); |
| 282 | struct mr_table *mrt; |
| 283 | struct mr_mfc *mfc; |
| 284 | |
| 285 | rcu_read_lock(); |
| 286 | for (mrt = iter(net, NULL); mrt; mrt = iter(net, mrt)) { |
| 287 | if (t < s_t) |
| 288 | goto next_table; |
| 289 | list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) { |
| 290 | if (e < s_e) |
| 291 | goto next_entry; |
| 292 | if (fill(mrt, skb, NETLINK_CB(cb->skb).portid, |
| 293 | cb->nlh->nlmsg_seq, mfc, |
| 294 | RTM_NEWROUTE, NLM_F_MULTI) < 0) |
| 295 | goto done; |
| 296 | next_entry: |
| 297 | e++; |
| 298 | } |
Yuval Mintz | 7b0db85 | 2018-02-28 23:29:39 +0200 | [diff] [blame] | 299 | |
| 300 | spin_lock_bh(lock); |
| 301 | list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) { |
| 302 | if (e < s_e) |
| 303 | goto next_entry2; |
| 304 | if (fill(mrt, skb, NETLINK_CB(cb->skb).portid, |
| 305 | cb->nlh->nlmsg_seq, mfc, |
| 306 | RTM_NEWROUTE, NLM_F_MULTI) < 0) { |
| 307 | spin_unlock_bh(lock); |
| 308 | goto done; |
| 309 | } |
| 310 | next_entry2: |
| 311 | e++; |
| 312 | } |
| 313 | spin_unlock_bh(lock); |
| 314 | e = 0; |
| 315 | s_e = 0; |
| 316 | next_table: |
| 317 | t++; |
| 318 | } |
| 319 | done: |
| 320 | rcu_read_unlock(); |
| 321 | |
| 322 | cb->args[1] = e; |
| 323 | cb->args[0] = t; |
| 324 | |
| 325 | return skb->len; |
| 326 | } |
| 327 | EXPORT_SYMBOL(mr_rtm_dumproute); |
Yuval Mintz | cdc9f94 | 2018-03-26 15:01:33 +0300 | [diff] [blame] | 328 | |
| 329 | int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family, |
| 330 | int (*rules_dump)(struct net *net, |
| 331 | struct notifier_block *nb), |
| 332 | struct mr_table *(*mr_iter)(struct net *net, |
| 333 | struct mr_table *mrt), |
| 334 | rwlock_t *mrt_lock) |
| 335 | { |
| 336 | struct mr_table *mrt; |
| 337 | int err; |
| 338 | |
| 339 | err = rules_dump(net, nb); |
| 340 | if (err) |
| 341 | return err; |
| 342 | |
| 343 | for (mrt = mr_iter(net, NULL); mrt; mrt = mr_iter(net, mrt)) { |
| 344 | struct vif_device *v = &mrt->vif_table[0]; |
| 345 | struct mr_mfc *mfc; |
| 346 | int vifi; |
| 347 | |
| 348 | /* Notifiy on table VIF entries */ |
| 349 | read_lock(mrt_lock); |
| 350 | for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) { |
| 351 | if (!v->dev) |
| 352 | continue; |
| 353 | |
| 354 | mr_call_vif_notifier(nb, net, family, |
| 355 | FIB_EVENT_VIF_ADD, |
| 356 | v, vifi, mrt->id); |
| 357 | } |
| 358 | read_unlock(mrt_lock); |
| 359 | |
| 360 | /* Notify on table MFC entries */ |
| 361 | list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) |
| 362 | mr_call_mfc_notifier(nb, net, family, |
| 363 | FIB_EVENT_ENTRY_ADD, |
| 364 | mfc, mrt->id); |
| 365 | } |
| 366 | |
| 367 | return 0; |
| 368 | } |
| 369 | EXPORT_SYMBOL(mr_dump); |