1 #ifndef __LINUX_MROUTE_BASE_H 2 #define __LINUX_MROUTE_BASE_H 3 4 #include <linux/netdevice.h> 5 #include <linux/rhashtable-types.h> 6 #include <linux/spinlock.h> 7 #include <net/net_namespace.h> 8 #include <net/sock.h> 9 #include <net/fib_notifier.h> 10 #include <net/ip_fib.h> 11 12 /** 13 * struct vif_device - interface representor for multicast routing 14 * @dev: network device being used 15 * @dev_tracker: refcount tracker for @dev reference 16 * @bytes_in: statistic; bytes ingressing 17 * @bytes_out: statistic; bytes egresing 18 * @pkt_in: statistic; packets ingressing 19 * @pkt_out: statistic; packets egressing 20 * @rate_limit: Traffic shaping (NI) 21 * @threshold: TTL threshold 22 * @flags: Control flags 23 * @link: Physical interface index 24 * @dev_parent_id: device parent id 25 * @local: Local address 26 * @remote: Remote address for tunnels 27 */ 28 struct vif_device { 29 struct net_device __rcu *dev; 30 netdevice_tracker dev_tracker; 31 unsigned long bytes_in, bytes_out; 32 unsigned long pkt_in, pkt_out; 33 unsigned long rate_limit; 34 unsigned char threshold; 35 unsigned short flags; 36 int link; 37 38 /* Currently only used by ipmr */ 39 struct netdev_phys_item_id dev_parent_id; 40 __be32 local, remote; 41 }; 42 43 struct vif_entry_notifier_info { 44 struct fib_notifier_info info; 45 struct net_device *dev; 46 unsigned short vif_index; 47 unsigned short vif_flags; 48 u32 tb_id; 49 }; 50 51 static inline int mr_call_vif_notifier(struct notifier_block *nb, 52 unsigned short family, 53 enum fib_event_type event_type, 54 struct vif_device *vif, 55 struct net_device *vif_dev, 56 unsigned short vif_index, u32 tb_id, 57 struct netlink_ext_ack *extack) 58 { 59 struct vif_entry_notifier_info info = { 60 .info = { 61 .family = family, 62 .extack = extack, 63 }, 64 .dev = vif_dev, 65 .vif_index = vif_index, 66 .vif_flags = vif->flags, 67 .tb_id = tb_id, 68 }; 69 70 return call_fib_notifier(nb, event_type, &info.info); 71 } 72 73 static inline int mr_call_vif_notifiers(struct net *net, 74 unsigned short family, 75 enum fib_event_type event_type, 76 struct vif_device *vif, 77 struct net_device *vif_dev, 78 unsigned short vif_index, u32 tb_id, 79 atomic_t *ipmr_seq) 80 { 81 struct vif_entry_notifier_info info = { 82 .info = { 83 .family = family, 84 }, 85 .dev = vif_dev, 86 .vif_index = vif_index, 87 .vif_flags = vif->flags, 88 .tb_id = tb_id, 89 }; 90 91 ASSERT_RTNL(); 92 atomic_inc(ipmr_seq); 93 return call_fib_notifiers(net, event_type, &info.info); 94 } 95 96 #ifndef MAXVIFS 97 /* This one is nasty; value is defined in uapi using different symbols for 98 * mroute and morute6 but both map into same 32. 99 */ 100 #define MAXVIFS 32 101 #endif 102 103 /* Note: This helper is deprecated. */ 104 #define VIF_EXISTS(_mrt, _idx) (!!rcu_access_pointer((_mrt)->vif_table[_idx].dev)) 105 106 /* mfc_flags: 107 * MFC_STATIC - the entry was added statically (not by a routing daemon) 108 * MFC_OFFLOAD - the entry was offloaded to the hardware 109 */ 110 enum { 111 MFC_STATIC = BIT(0), 112 MFC_OFFLOAD = BIT(1), 113 }; 114 115 /** 116 * struct mr_mfc - common multicast routing entries 117 * @mnode: rhashtable list 118 * @mfc_parent: source interface (iif) 119 * @mfc_flags: entry flags 120 * @expires: unresolved entry expire time 121 * @unresolved: unresolved cached skbs 122 * @last_assert: time of last assert 123 * @minvif: minimum VIF id 124 * @maxvif: maximum VIF id 125 * @bytes: bytes that have passed for this entry 126 * @pkt: packets that have passed for this entry 127 * @wrong_if: number of wrong source interface hits 128 * @lastuse: time of last use of the group (traffic or update) 129 * @ttls: OIF TTL threshold array 130 * @refcount: reference count for this entry 131 * @list: global entry list 132 * @rcu: used for entry destruction 133 * @free: Operation used for freeing an entry under RCU 134 */ 135 struct mr_mfc { 136 struct rhlist_head mnode; 137 unsigned short mfc_parent; 138 int mfc_flags; 139 140 union { 141 struct { 142 unsigned long expires; 143 struct sk_buff_head unresolved; 144 } unres; 145 struct { 146 unsigned long last_assert; 147 int minvif; 148 int maxvif; 149 atomic_long_t bytes; 150 atomic_long_t pkt; 151 atomic_long_t wrong_if; 152 unsigned long lastuse; 153 unsigned char ttls[MAXVIFS]; 154 refcount_t refcount; 155 } res; 156 } mfc_un; 157 struct list_head list; 158 struct rcu_head rcu; 159 void (*free)(struct rcu_head *head); 160 }; 161 162 static inline void mr_cache_put(struct mr_mfc *c) 163 { 164 if (refcount_dec_and_test(&c->mfc_un.res.refcount)) 165 call_rcu(&c->rcu, c->free); 166 } 167 168 static inline void mr_cache_hold(struct mr_mfc *c) 169 { 170 refcount_inc(&c->mfc_un.res.refcount); 171 } 172 173 struct mfc_entry_notifier_info { 174 struct fib_notifier_info info; 175 struct mr_mfc *mfc; 176 u32 tb_id; 177 }; 178 179 static inline int mr_call_mfc_notifier(struct notifier_block *nb, 180 unsigned short family, 181 enum fib_event_type event_type, 182 struct mr_mfc *mfc, u32 tb_id, 183 struct netlink_ext_ack *extack) 184 { 185 struct mfc_entry_notifier_info info = { 186 .info = { 187 .family = family, 188 .extack = extack, 189 }, 190 .mfc = mfc, 191 .tb_id = tb_id 192 }; 193 194 return call_fib_notifier(nb, event_type, &info.info); 195 } 196 197 static inline int mr_call_mfc_notifiers(struct net *net, 198 unsigned short family, 199 enum fib_event_type event_type, 200 struct mr_mfc *mfc, u32 tb_id, 201 atomic_t *ipmr_seq) 202 { 203 struct mfc_entry_notifier_info info = { 204 .info = { 205 .family = family, 206 }, 207 .mfc = mfc, 208 .tb_id = tb_id 209 }; 210 211 atomic_inc(ipmr_seq); 212 return call_fib_notifiers(net, event_type, &info.info); 213 } 214 215 struct mr_table; 216 217 /** 218 * struct mr_table_ops - callbacks and info for protocol-specific ops 219 * @rht_params: parameters for accessing the MFC hash 220 * @cmparg_any: a hash key to be used for matching on (*,*) routes 221 */ 222 struct mr_table_ops { 223 const struct rhashtable_params *rht_params; 224 void *cmparg_any; 225 }; 226 227 /** 228 * struct mr_table - a multicast routing table 229 * @list: entry within a list of multicast routing tables 230 * @net: net where this table belongs 231 * @ops: protocol specific operations 232 * @id: identifier of the table 233 * @mroute_sk: socket associated with the table 234 * @ipmr_expire_timer: timer for handling unresolved routes 235 * @mfc_unres_queue: list of unresolved MFC entries 236 * @vif_table: array containing all possible vifs 237 * @mfc_hash: Hash table of all resolved routes for easy lookup 238 * @mfc_cache_list: list of resovled routes for possible traversal 239 * @maxvif: Identifier of highest value vif currently in use 240 * @cache_resolve_queue_len: current size of unresolved queue 241 * @mroute_do_assert: Whether to inform userspace on wrong ingress 242 * @mroute_do_pim: Whether to receive IGMP PIMv1 243 * @mroute_reg_vif_num: PIM-device vif index 244 */ 245 struct mr_table { 246 struct list_head list; 247 possible_net_t net; 248 struct mr_table_ops ops; 249 u32 id; 250 struct sock __rcu *mroute_sk; 251 struct timer_list ipmr_expire_timer; 252 struct list_head mfc_unres_queue; 253 struct vif_device vif_table[MAXVIFS]; 254 struct rhltable mfc_hash; 255 struct list_head mfc_cache_list; 256 int maxvif; 257 atomic_t cache_resolve_queue_len; 258 bool mroute_do_assert; 259 bool mroute_do_pim; 260 bool mroute_do_wrvifwhole; 261 int mroute_reg_vif_num; 262 }; 263 264 static inline bool mr_can_free_table(struct net *net) 265 { 266 return !check_net(net) || !net_initialized(net); 267 } 268 269 #ifdef CONFIG_IP_MROUTE_COMMON 270 void vif_device_init(struct vif_device *v, 271 struct net_device *dev, 272 unsigned long rate_limit, 273 unsigned char threshold, 274 unsigned short flags, 275 unsigned short get_iflink_mask); 276 277 struct mr_table * 278 mr_table_alloc(struct net *net, u32 id, 279 struct mr_table_ops *ops, 280 void (*expire_func)(struct timer_list *t), 281 void (*table_set)(struct mr_table *mrt, 282 struct net *net)); 283 284 /* These actually return 'struct mr_mfc *', but to avoid need for explicit 285 * castings they simply return void. 286 */ 287 void *mr_mfc_find_parent(struct mr_table *mrt, 288 void *hasharg, int parent); 289 void *mr_mfc_find_any_parent(struct mr_table *mrt, int vifi); 290 void *mr_mfc_find_any(struct mr_table *mrt, int vifi, void *hasharg); 291 292 int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 293 struct mr_mfc *c, struct rtmsg *rtm); 294 int mr_table_dump(struct mr_table *mrt, struct sk_buff *skb, 295 struct netlink_callback *cb, 296 int (*fill)(struct mr_table *mrt, struct sk_buff *skb, 297 u32 portid, u32 seq, struct mr_mfc *c, 298 int cmd, int flags), 299 spinlock_t *lock, struct fib_dump_filter *filter); 300 int mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb, 301 struct mr_table *(*iter)(struct net *net, 302 struct mr_table *mrt), 303 int (*fill)(struct mr_table *mrt, 304 struct sk_buff *skb, 305 u32 portid, u32 seq, struct mr_mfc *c, 306 int cmd, int flags), 307 spinlock_t *lock, struct fib_dump_filter *filter); 308 309 int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family, 310 int (*rules_dump)(struct net *net, 311 struct notifier_block *nb, 312 struct netlink_ext_ack *extack), 313 struct mr_table *(*mr_iter)(struct net *net, 314 struct mr_table *mrt), 315 struct netlink_ext_ack *extack); 316 #else 317 static inline void vif_device_init(struct vif_device *v, 318 struct net_device *dev, 319 unsigned long rate_limit, 320 unsigned char threshold, 321 unsigned short flags, 322 unsigned short get_iflink_mask) 323 { 324 } 325 326 static inline void *mr_mfc_find_parent(struct mr_table *mrt, 327 void *hasharg, int parent) 328 { 329 return NULL; 330 } 331 332 static inline void *mr_mfc_find_any_parent(struct mr_table *mrt, 333 int vifi) 334 { 335 return NULL; 336 } 337 338 static inline struct mr_mfc *mr_mfc_find_any(struct mr_table *mrt, 339 int vifi, void *hasharg) 340 { 341 return NULL; 342 } 343 344 static inline int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb, 345 struct mr_mfc *c, struct rtmsg *rtm) 346 { 347 return -EINVAL; 348 } 349 350 static inline int 351 mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb, 352 struct mr_table *(*iter)(struct net *net, 353 struct mr_table *mrt), 354 int (*fill)(struct mr_table *mrt, 355 struct sk_buff *skb, 356 u32 portid, u32 seq, struct mr_mfc *c, 357 int cmd, int flags), 358 spinlock_t *lock, struct fib_dump_filter *filter) 359 { 360 return -EINVAL; 361 } 362 363 static inline int mr_dump(struct net *net, struct notifier_block *nb, 364 unsigned short family, 365 int (*rules_dump)(struct net *net, 366 struct notifier_block *nb, 367 struct netlink_ext_ack *extack), 368 struct mr_table *(*mr_iter)(struct net *net, 369 struct mr_table *mrt), 370 struct netlink_ext_ack *extack) 371 { 372 return -EINVAL; 373 } 374 #endif 375 376 static inline void *mr_mfc_find(struct mr_table *mrt, void *hasharg) 377 { 378 return mr_mfc_find_parent(mrt, hasharg, -1); 379 } 380 381 #ifdef CONFIG_PROC_FS 382 struct mr_vif_iter { 383 struct seq_net_private p; 384 struct mr_table *mrt; 385 int ct; 386 }; 387 388 struct mr_mfc_iter { 389 struct seq_net_private p; 390 struct mr_table *mrt; 391 struct list_head *cache; 392 393 /* Lock protecting the mr_table's unresolved queue */ 394 spinlock_t *lock; 395 }; 396 397 #ifdef CONFIG_IP_MROUTE_COMMON 398 void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter, loff_t pos); 399 void *mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos); 400 401 static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos) 402 { 403 return *pos ? mr_vif_seq_idx(seq_file_net(seq), 404 seq->private, *pos - 1) 405 : SEQ_START_TOKEN; 406 } 407 408 /* These actually return 'struct mr_mfc *', but to avoid need for explicit 409 * castings they simply return void. 410 */ 411 void *mr_mfc_seq_idx(struct net *net, 412 struct mr_mfc_iter *it, loff_t pos); 413 void *mr_mfc_seq_next(struct seq_file *seq, void *v, 414 loff_t *pos); 415 416 static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos, 417 struct mr_table *mrt, spinlock_t *lock) 418 { 419 struct mr_mfc_iter *it = seq->private; 420 421 it->mrt = mrt; 422 it->cache = NULL; 423 it->lock = lock; 424 425 return *pos ? mr_mfc_seq_idx(seq_file_net(seq), 426 seq->private, *pos - 1) 427 : SEQ_START_TOKEN; 428 } 429 430 static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v) 431 { 432 struct mr_mfc_iter *it = seq->private; 433 struct mr_table *mrt = it->mrt; 434 435 if (it->cache == &mrt->mfc_unres_queue) 436 spin_unlock_bh(it->lock); 437 else if (it->cache == &mrt->mfc_cache_list) 438 rcu_read_unlock(); 439 } 440 #else 441 static inline void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter, 442 loff_t pos) 443 { 444 return NULL; 445 } 446 447 static inline void *mr_vif_seq_next(struct seq_file *seq, 448 void *v, loff_t *pos) 449 { 450 return NULL; 451 } 452 453 static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos) 454 { 455 return NULL; 456 } 457 458 static inline void *mr_mfc_seq_idx(struct net *net, 459 struct mr_mfc_iter *it, loff_t pos) 460 { 461 return NULL; 462 } 463 464 static inline void *mr_mfc_seq_next(struct seq_file *seq, void *v, 465 loff_t *pos) 466 { 467 return NULL; 468 } 469 470 static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos, 471 struct mr_table *mrt, spinlock_t *lock) 472 { 473 return NULL; 474 } 475 476 static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v) 477 { 478 } 479 #endif 480 #endif 481 #endif 482