mroute_base.h 12 KB

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