mroute_base.h 12 KB

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