mroute_base.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  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. /**
  10. * struct vif_device - interface representor for multicast routing
  11. * @dev: network device being used
  12. * @bytes_in: statistic; bytes ingressing
  13. * @bytes_out: statistic; bytes egresing
  14. * @pkt_in: statistic; packets ingressing
  15. * @pkt_out: statistic; packets egressing
  16. * @rate_limit: Traffic shaping (NI)
  17. * @threshold: TTL threshold
  18. * @flags: Control flags
  19. * @link: Physical interface index
  20. * @dev_parent_id: device parent id
  21. * @local: Local address
  22. * @remote: Remote address for tunnels
  23. */
  24. struct vif_device {
  25. struct net_device *dev;
  26. unsigned long bytes_in, bytes_out;
  27. unsigned long pkt_in, pkt_out;
  28. unsigned long rate_limit;
  29. unsigned char threshold;
  30. unsigned short flags;
  31. int link;
  32. /* Currently only used by ipmr */
  33. struct netdev_phys_item_id dev_parent_id;
  34. __be32 local, remote;
  35. };
  36. struct vif_entry_notifier_info {
  37. struct fib_notifier_info info;
  38. struct net_device *dev;
  39. unsigned short vif_index;
  40. unsigned short vif_flags;
  41. u32 tb_id;
  42. };
  43. static inline int mr_call_vif_notifier(struct notifier_block *nb,
  44. struct net *net,
  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. {
  50. struct vif_entry_notifier_info info = {
  51. .info = {
  52. .family = family,
  53. .net = net,
  54. },
  55. .dev = vif->dev,
  56. .vif_index = vif_index,
  57. .vif_flags = vif->flags,
  58. .tb_id = tb_id,
  59. };
  60. return call_fib_notifier(nb, net, event_type, &info.info);
  61. }
  62. static inline int mr_call_vif_notifiers(struct net *net,
  63. unsigned short family,
  64. enum fib_event_type event_type,
  65. struct vif_device *vif,
  66. unsigned short vif_index, u32 tb_id,
  67. unsigned int *ipmr_seq)
  68. {
  69. struct vif_entry_notifier_info info = {
  70. .info = {
  71. .family = family,
  72. .net = net,
  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. struct net *net,
  159. unsigned short family,
  160. enum fib_event_type event_type,
  161. struct mr_mfc *mfc, u32 tb_id)
  162. {
  163. struct mfc_entry_notifier_info info = {
  164. .info = {
  165. .family = family,
  166. .net = net,
  167. },
  168. .mfc = mfc,
  169. .tb_id = tb_id
  170. };
  171. return call_fib_notifier(nb, net, 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. .net = net,
  183. },
  184. .mfc = mfc,
  185. .tb_id = tb_id
  186. };
  187. ASSERT_RTNL();
  188. (*ipmr_seq)++;
  189. return call_fib_notifiers(net, event_type, &info.info);
  190. }
  191. struct mr_table;
  192. /**
  193. * struct mr_table_ops - callbacks and info for protocol-specific ops
  194. * @rht_params: parameters for accessing the MFC hash
  195. * @cmparg_any: a hash key to be used for matching on (*,*) routes
  196. */
  197. struct mr_table_ops {
  198. const struct rhashtable_params *rht_params;
  199. void *cmparg_any;
  200. };
  201. /**
  202. * struct mr_table - a multicast routing table
  203. * @list: entry within a list of multicast routing tables
  204. * @net: net where this table belongs
  205. * @ops: protocol specific operations
  206. * @id: identifier of the table
  207. * @mroute_sk: socket associated with the table
  208. * @ipmr_expire_timer: timer for handling unresolved routes
  209. * @mfc_unres_queue: list of unresolved MFC entries
  210. * @vif_table: array containing all possible vifs
  211. * @mfc_hash: Hash table of all resolved routes for easy lookup
  212. * @mfc_cache_list: list of resovled routes for possible traversal
  213. * @maxvif: Identifier of highest value vif currently in use
  214. * @cache_resolve_queue_len: current size of unresolved queue
  215. * @mroute_do_assert: Whether to inform userspace on wrong ingress
  216. * @mroute_do_pim: Whether to receive IGMP PIMv1
  217. * @mroute_reg_vif_num: PIM-device vif index
  218. */
  219. struct mr_table {
  220. struct list_head list;
  221. possible_net_t net;
  222. struct mr_table_ops ops;
  223. u32 id;
  224. struct sock __rcu *mroute_sk;
  225. struct timer_list ipmr_expire_timer;
  226. struct list_head mfc_unres_queue;
  227. struct vif_device vif_table[MAXVIFS];
  228. struct rhltable mfc_hash;
  229. struct list_head mfc_cache_list;
  230. int maxvif;
  231. atomic_t cache_resolve_queue_len;
  232. bool mroute_do_assert;
  233. bool mroute_do_pim;
  234. bool mroute_do_wrvifwhole;
  235. int mroute_reg_vif_num;
  236. };
  237. #ifdef CONFIG_IP_MROUTE_COMMON
  238. void vif_device_init(struct vif_device *v,
  239. struct net_device *dev,
  240. unsigned long rate_limit,
  241. unsigned char threshold,
  242. unsigned short flags,
  243. unsigned short get_iflink_mask);
  244. struct mr_table *
  245. mr_table_alloc(struct net *net, u32 id,
  246. struct mr_table_ops *ops,
  247. void (*expire_func)(struct timer_list *t),
  248. void (*table_set)(struct mr_table *mrt,
  249. struct net *net));
  250. /* These actually return 'struct mr_mfc *', but to avoid need for explicit
  251. * castings they simply return void.
  252. */
  253. void *mr_mfc_find_parent(struct mr_table *mrt,
  254. void *hasharg, int parent);
  255. void *mr_mfc_find_any_parent(struct mr_table *mrt, int vifi);
  256. void *mr_mfc_find_any(struct mr_table *mrt, int vifi, void *hasharg);
  257. int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  258. struct mr_mfc *c, struct rtmsg *rtm);
  259. int mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
  260. struct mr_table *(*iter)(struct net *net,
  261. struct mr_table *mrt),
  262. int (*fill)(struct mr_table *mrt,
  263. struct sk_buff *skb,
  264. u32 portid, u32 seq, struct mr_mfc *c,
  265. int cmd, int flags),
  266. spinlock_t *lock);
  267. int mr_dump(struct net *net, struct notifier_block *nb, unsigned short family,
  268. int (*rules_dump)(struct net *net,
  269. struct notifier_block *nb),
  270. struct mr_table *(*mr_iter)(struct net *net,
  271. struct mr_table *mrt),
  272. rwlock_t *mrt_lock);
  273. #else
  274. static inline void vif_device_init(struct vif_device *v,
  275. struct net_device *dev,
  276. unsigned long rate_limit,
  277. unsigned char threshold,
  278. unsigned short flags,
  279. unsigned short get_iflink_mask)
  280. {
  281. }
  282. static inline void *mr_mfc_find_parent(struct mr_table *mrt,
  283. void *hasharg, int parent)
  284. {
  285. return NULL;
  286. }
  287. static inline void *mr_mfc_find_any_parent(struct mr_table *mrt,
  288. int vifi)
  289. {
  290. return NULL;
  291. }
  292. static inline struct mr_mfc *mr_mfc_find_any(struct mr_table *mrt,
  293. int vifi, void *hasharg)
  294. {
  295. return NULL;
  296. }
  297. static inline int mr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
  298. struct mr_mfc *c, struct rtmsg *rtm)
  299. {
  300. return -EINVAL;
  301. }
  302. static inline int
  303. mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb,
  304. struct mr_table *(*iter)(struct net *net,
  305. struct mr_table *mrt),
  306. int (*fill)(struct mr_table *mrt,
  307. struct sk_buff *skb,
  308. u32 portid, u32 seq, struct mr_mfc *c,
  309. int cmd, int flags),
  310. spinlock_t *lock)
  311. {
  312. return -EINVAL;
  313. }
  314. static inline int mr_dump(struct net *net, struct notifier_block *nb,
  315. unsigned short family,
  316. int (*rules_dump)(struct net *net,
  317. struct notifier_block *nb),
  318. struct mr_table *(*mr_iter)(struct net *net,
  319. struct mr_table *mrt),
  320. rwlock_t *mrt_lock)
  321. {
  322. return -EINVAL;
  323. }
  324. #endif
  325. static inline void *mr_mfc_find(struct mr_table *mrt, void *hasharg)
  326. {
  327. return mr_mfc_find_parent(mrt, hasharg, -1);
  328. }
  329. #ifdef CONFIG_PROC_FS
  330. struct mr_vif_iter {
  331. struct seq_net_private p;
  332. struct mr_table *mrt;
  333. int ct;
  334. };
  335. struct mr_mfc_iter {
  336. struct seq_net_private p;
  337. struct mr_table *mrt;
  338. struct list_head *cache;
  339. /* Lock protecting the mr_table's unresolved queue */
  340. spinlock_t *lock;
  341. };
  342. #ifdef CONFIG_IP_MROUTE_COMMON
  343. void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter, loff_t pos);
  344. void *mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos);
  345. static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
  346. {
  347. return *pos ? mr_vif_seq_idx(seq_file_net(seq),
  348. seq->private, *pos - 1)
  349. : SEQ_START_TOKEN;
  350. }
  351. /* These actually return 'struct mr_mfc *', but to avoid need for explicit
  352. * castings they simply return void.
  353. */
  354. void *mr_mfc_seq_idx(struct net *net,
  355. struct mr_mfc_iter *it, loff_t pos);
  356. void *mr_mfc_seq_next(struct seq_file *seq, void *v,
  357. loff_t *pos);
  358. static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
  359. struct mr_table *mrt, spinlock_t *lock)
  360. {
  361. struct mr_mfc_iter *it = seq->private;
  362. it->mrt = mrt;
  363. it->cache = NULL;
  364. it->lock = lock;
  365. return *pos ? mr_mfc_seq_idx(seq_file_net(seq),
  366. seq->private, *pos - 1)
  367. : SEQ_START_TOKEN;
  368. }
  369. static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)
  370. {
  371. struct mr_mfc_iter *it = seq->private;
  372. struct mr_table *mrt = it->mrt;
  373. if (it->cache == &mrt->mfc_unres_queue)
  374. spin_unlock_bh(it->lock);
  375. else if (it->cache == &mrt->mfc_cache_list)
  376. rcu_read_unlock();
  377. }
  378. #else
  379. static inline void *mr_vif_seq_idx(struct net *net, struct mr_vif_iter *iter,
  380. loff_t pos)
  381. {
  382. return NULL;
  383. }
  384. static inline void *mr_vif_seq_next(struct seq_file *seq,
  385. void *v, loff_t *pos)
  386. {
  387. return NULL;
  388. }
  389. static inline void *mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
  390. {
  391. return NULL;
  392. }
  393. static inline void *mr_mfc_seq_idx(struct net *net,
  394. struct mr_mfc_iter *it, loff_t pos)
  395. {
  396. return NULL;
  397. }
  398. static inline void *mr_mfc_seq_next(struct seq_file *seq, void *v,
  399. loff_t *pos)
  400. {
  401. return NULL;
  402. }
  403. static inline void *mr_mfc_seq_start(struct seq_file *seq, loff_t *pos,
  404. struct mr_table *mrt, spinlock_t *lock)
  405. {
  406. return NULL;
  407. }
  408. static inline void mr_mfc_seq_stop(struct seq_file *seq, void *v)
  409. {
  410. }
  411. #endif
  412. #endif
  413. #endif