br_private.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * Linux ethernet bridge
  3. *
  4. * Authors:
  5. * Lennert Buytenhek <buytenh@gnu.org>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #ifndef _BR_PRIVATE_H
  13. #define _BR_PRIVATE_H
  14. #include <linux/netdevice.h>
  15. #include <linux/if_bridge.h>
  16. #include <linux/netpoll.h>
  17. #include <linux/u64_stats_sync.h>
  18. #include <net/route.h>
  19. #define BR_HASH_BITS 8
  20. #define BR_HASH_SIZE (1 << BR_HASH_BITS)
  21. #define BR_HOLD_TIME (1*HZ)
  22. #define BR_PORT_BITS 10
  23. #define BR_MAX_PORTS (1<<BR_PORT_BITS)
  24. #define BR_VERSION "2.3"
  25. /* Control of forwarding link local multicast */
  26. #define BR_GROUPFWD_DEFAULT 0
  27. /* Don't allow forwarding control protocols like STP and LLDP */
  28. #define BR_GROUPFWD_RESTRICTED 0x4007u
  29. /* Path to usermode spanning tree program */
  30. #define BR_STP_PROG "/sbin/bridge-stp"
  31. typedef struct bridge_id bridge_id;
  32. typedef struct mac_addr mac_addr;
  33. typedef __u16 port_id;
  34. struct bridge_id
  35. {
  36. unsigned char prio[2];
  37. unsigned char addr[6];
  38. };
  39. struct mac_addr
  40. {
  41. unsigned char addr[6];
  42. };
  43. struct br_ip
  44. {
  45. union {
  46. __be32 ip4;
  47. #if IS_ENABLED(CONFIG_IPV6)
  48. struct in6_addr ip6;
  49. #endif
  50. } u;
  51. __be16 proto;
  52. };
  53. struct net_bridge_fdb_entry
  54. {
  55. struct hlist_node hlist;
  56. struct net_bridge_port *dst;
  57. struct rcu_head rcu;
  58. unsigned long updated;
  59. unsigned long used;
  60. mac_addr addr;
  61. unsigned char is_local;
  62. unsigned char is_static;
  63. };
  64. struct net_bridge_port_group {
  65. struct net_bridge_port *port;
  66. struct net_bridge_port_group __rcu *next;
  67. struct hlist_node mglist;
  68. struct rcu_head rcu;
  69. struct timer_list timer;
  70. struct br_ip addr;
  71. };
  72. struct net_bridge_mdb_entry
  73. {
  74. struct hlist_node hlist[2];
  75. struct net_bridge *br;
  76. struct net_bridge_port_group __rcu *ports;
  77. struct rcu_head rcu;
  78. struct timer_list timer;
  79. struct br_ip addr;
  80. bool mglist;
  81. };
  82. struct net_bridge_mdb_htable
  83. {
  84. struct hlist_head *mhash;
  85. struct rcu_head rcu;
  86. struct net_bridge_mdb_htable *old;
  87. u32 size;
  88. u32 max;
  89. u32 secret;
  90. u32 ver;
  91. };
  92. struct net_bridge_port
  93. {
  94. struct net_bridge *br;
  95. struct net_device *dev;
  96. struct list_head list;
  97. /* STP */
  98. u8 priority;
  99. u8 state;
  100. u16 port_no;
  101. unsigned char topology_change_ack;
  102. unsigned char config_pending;
  103. port_id port_id;
  104. port_id designated_port;
  105. bridge_id designated_root;
  106. bridge_id designated_bridge;
  107. u32 path_cost;
  108. u32 designated_cost;
  109. unsigned long designated_age;
  110. struct timer_list forward_delay_timer;
  111. struct timer_list hold_timer;
  112. struct timer_list message_age_timer;
  113. struct kobject kobj;
  114. struct rcu_head rcu;
  115. unsigned long flags;
  116. #define BR_HAIRPIN_MODE 0x00000001
  117. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  118. u32 multicast_startup_queries_sent;
  119. unsigned char multicast_router;
  120. struct timer_list multicast_router_timer;
  121. struct timer_list multicast_query_timer;
  122. struct hlist_head mglist;
  123. struct hlist_node rlist;
  124. #endif
  125. #ifdef CONFIG_SYSFS
  126. char sysfs_name[IFNAMSIZ];
  127. #endif
  128. #ifdef CONFIG_NET_POLL_CONTROLLER
  129. struct netpoll *np;
  130. #endif
  131. };
  132. #define br_port_exists(dev) (dev->priv_flags & IFF_BRIDGE_PORT)
  133. static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
  134. {
  135. struct net_bridge_port *port = rcu_dereference(dev->rx_handler_data);
  136. return br_port_exists(dev) ? port : NULL;
  137. }
  138. static inline struct net_bridge_port *br_port_get_rtnl(struct net_device *dev)
  139. {
  140. return br_port_exists(dev) ?
  141. rtnl_dereference(dev->rx_handler_data) : NULL;
  142. }
  143. struct br_cpu_netstats {
  144. u64 rx_packets;
  145. u64 rx_bytes;
  146. u64 tx_packets;
  147. u64 tx_bytes;
  148. struct u64_stats_sync syncp;
  149. };
  150. struct net_bridge
  151. {
  152. spinlock_t lock;
  153. struct list_head port_list;
  154. struct net_device *dev;
  155. struct br_cpu_netstats __percpu *stats;
  156. spinlock_t hash_lock;
  157. struct hlist_head hash[BR_HASH_SIZE];
  158. #ifdef CONFIG_BRIDGE_NETFILTER
  159. struct rtable fake_rtable;
  160. bool nf_call_iptables;
  161. bool nf_call_ip6tables;
  162. bool nf_call_arptables;
  163. #endif
  164. unsigned long flags;
  165. #define BR_SET_MAC_ADDR 0x00000001
  166. u16 group_fwd_mask;
  167. /* STP */
  168. bridge_id designated_root;
  169. bridge_id bridge_id;
  170. u32 root_path_cost;
  171. unsigned long max_age;
  172. unsigned long hello_time;
  173. unsigned long forward_delay;
  174. unsigned long bridge_max_age;
  175. unsigned long ageing_time;
  176. unsigned long bridge_hello_time;
  177. unsigned long bridge_forward_delay;
  178. u8 group_addr[ETH_ALEN];
  179. u16 root_port;
  180. enum {
  181. BR_NO_STP, /* no spanning tree */
  182. BR_KERNEL_STP, /* old STP in kernel */
  183. BR_USER_STP, /* new RSTP in userspace */
  184. } stp_enabled;
  185. unsigned char topology_change;
  186. unsigned char topology_change_detected;
  187. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  188. unsigned char multicast_router;
  189. u8 multicast_disabled:1;
  190. u32 hash_elasticity;
  191. u32 hash_max;
  192. u32 multicast_last_member_count;
  193. u32 multicast_startup_queries_sent;
  194. u32 multicast_startup_query_count;
  195. unsigned long multicast_last_member_interval;
  196. unsigned long multicast_membership_interval;
  197. unsigned long multicast_querier_interval;
  198. unsigned long multicast_query_interval;
  199. unsigned long multicast_query_response_interval;
  200. unsigned long multicast_startup_query_interval;
  201. spinlock_t multicast_lock;
  202. struct net_bridge_mdb_htable __rcu *mdb;
  203. struct hlist_head router_list;
  204. struct timer_list multicast_router_timer;
  205. struct timer_list multicast_querier_timer;
  206. struct timer_list multicast_query_timer;
  207. #endif
  208. struct timer_list hello_timer;
  209. struct timer_list tcn_timer;
  210. struct timer_list topology_change_timer;
  211. struct timer_list gc_timer;
  212. struct kobject *ifobj;
  213. };
  214. struct br_input_skb_cb {
  215. struct net_device *brdev;
  216. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  217. int igmp;
  218. int mrouters_only;
  219. #endif
  220. };
  221. #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb)
  222. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  223. # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only)
  224. #else
  225. # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0)
  226. #endif
  227. #define br_printk(level, br, format, args...) \
  228. printk(level "%s: " format, (br)->dev->name, ##args)
  229. #define br_err(__br, format, args...) \
  230. br_printk(KERN_ERR, __br, format, ##args)
  231. #define br_warn(__br, format, args...) \
  232. br_printk(KERN_WARNING, __br, format, ##args)
  233. #define br_notice(__br, format, args...) \
  234. br_printk(KERN_NOTICE, __br, format, ##args)
  235. #define br_info(__br, format, args...) \
  236. br_printk(KERN_INFO, __br, format, ##args)
  237. #define br_debug(br, format, args...) \
  238. pr_debug("%s: " format, (br)->dev->name, ##args)
  239. extern struct notifier_block br_device_notifier;
  240. extern const u8 br_group_address[ETH_ALEN];
  241. /* called under bridge lock */
  242. static inline int br_is_root_bridge(const struct net_bridge *br)
  243. {
  244. return !memcmp(&br->bridge_id, &br->designated_root, 8);
  245. }
  246. /* br_device.c */
  247. extern void br_dev_setup(struct net_device *dev);
  248. extern void br_dev_delete(struct net_device *dev, struct list_head *list);
  249. extern netdev_tx_t br_dev_xmit(struct sk_buff *skb,
  250. struct net_device *dev);
  251. #ifdef CONFIG_NET_POLL_CONTROLLER
  252. static inline struct netpoll_info *br_netpoll_info(struct net_bridge *br)
  253. {
  254. return br->dev->npinfo;
  255. }
  256. static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
  257. struct sk_buff *skb)
  258. {
  259. struct netpoll *np = p->np;
  260. if (np)
  261. netpoll_send_skb(np, skb);
  262. }
  263. extern int br_netpoll_enable(struct net_bridge_port *p);
  264. extern void br_netpoll_disable(struct net_bridge_port *p);
  265. #else
  266. static inline struct netpoll_info *br_netpoll_info(struct net_bridge *br)
  267. {
  268. return NULL;
  269. }
  270. static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
  271. struct sk_buff *skb)
  272. {
  273. }
  274. static inline int br_netpoll_enable(struct net_bridge_port *p)
  275. {
  276. return 0;
  277. }
  278. static inline void br_netpoll_disable(struct net_bridge_port *p)
  279. {
  280. }
  281. #endif
  282. /* br_fdb.c */
  283. extern int br_fdb_init(void);
  284. extern void br_fdb_fini(void);
  285. extern void br_fdb_flush(struct net_bridge *br);
  286. extern void br_fdb_changeaddr(struct net_bridge_port *p,
  287. const unsigned char *newaddr);
  288. extern void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
  289. extern void br_fdb_cleanup(unsigned long arg);
  290. extern void br_fdb_delete_by_port(struct net_bridge *br,
  291. const struct net_bridge_port *p, int do_all);
  292. extern struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
  293. const unsigned char *addr);
  294. extern int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
  295. extern int br_fdb_fillbuf(struct net_bridge *br, void *buf,
  296. unsigned long count, unsigned long off);
  297. extern int br_fdb_insert(struct net_bridge *br,
  298. struct net_bridge_port *source,
  299. const unsigned char *addr);
  300. extern void br_fdb_update(struct net_bridge *br,
  301. struct net_bridge_port *source,
  302. const unsigned char *addr);
  303. extern int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb);
  304. extern int br_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  305. extern int br_fdb_delete(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg);
  306. /* br_forward.c */
  307. extern void br_deliver(const struct net_bridge_port *to,
  308. struct sk_buff *skb);
  309. extern int br_dev_queue_push_xmit(struct sk_buff *skb);
  310. extern void br_forward(const struct net_bridge_port *to,
  311. struct sk_buff *skb, struct sk_buff *skb0);
  312. extern int br_forward_finish(struct sk_buff *skb);
  313. extern void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb);
  314. extern void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
  315. struct sk_buff *skb2);
  316. /* br_if.c */
  317. extern void br_port_carrier_check(struct net_bridge_port *p);
  318. extern int br_add_bridge(struct net *net, const char *name);
  319. extern int br_del_bridge(struct net *net, const char *name);
  320. extern void br_net_exit(struct net *net);
  321. extern int br_add_if(struct net_bridge *br,
  322. struct net_device *dev);
  323. extern int br_del_if(struct net_bridge *br,
  324. struct net_device *dev);
  325. extern int br_min_mtu(const struct net_bridge *br);
  326. extern netdev_features_t br_features_recompute(struct net_bridge *br,
  327. netdev_features_t features);
  328. /* br_input.c */
  329. extern int br_handle_frame_finish(struct sk_buff *skb);
  330. extern rx_handler_result_t br_handle_frame(struct sk_buff **pskb);
  331. /* br_ioctl.c */
  332. extern int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  333. extern int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd, void __user *arg);
  334. /* br_multicast.c */
  335. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  336. extern int br_multicast_rcv(struct net_bridge *br,
  337. struct net_bridge_port *port,
  338. struct sk_buff *skb);
  339. extern struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
  340. struct sk_buff *skb);
  341. extern void br_multicast_add_port(struct net_bridge_port *port);
  342. extern void br_multicast_del_port(struct net_bridge_port *port);
  343. extern void br_multicast_enable_port(struct net_bridge_port *port);
  344. extern void br_multicast_disable_port(struct net_bridge_port *port);
  345. extern void br_multicast_init(struct net_bridge *br);
  346. extern void br_multicast_open(struct net_bridge *br);
  347. extern void br_multicast_stop(struct net_bridge *br);
  348. extern void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
  349. struct sk_buff *skb);
  350. extern void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
  351. struct sk_buff *skb, struct sk_buff *skb2);
  352. extern int br_multicast_set_router(struct net_bridge *br, unsigned long val);
  353. extern int br_multicast_set_port_router(struct net_bridge_port *p,
  354. unsigned long val);
  355. extern int br_multicast_toggle(struct net_bridge *br, unsigned long val);
  356. extern int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
  357. static inline bool br_multicast_is_router(struct net_bridge *br)
  358. {
  359. return br->multicast_router == 2 ||
  360. (br->multicast_router == 1 &&
  361. timer_pending(&br->multicast_router_timer));
  362. }
  363. #else
  364. static inline int br_multicast_rcv(struct net_bridge *br,
  365. struct net_bridge_port *port,
  366. struct sk_buff *skb)
  367. {
  368. return 0;
  369. }
  370. static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
  371. struct sk_buff *skb)
  372. {
  373. return NULL;
  374. }
  375. static inline void br_multicast_add_port(struct net_bridge_port *port)
  376. {
  377. }
  378. static inline void br_multicast_del_port(struct net_bridge_port *port)
  379. {
  380. }
  381. static inline void br_multicast_enable_port(struct net_bridge_port *port)
  382. {
  383. }
  384. static inline void br_multicast_disable_port(struct net_bridge_port *port)
  385. {
  386. }
  387. static inline void br_multicast_init(struct net_bridge *br)
  388. {
  389. }
  390. static inline void br_multicast_open(struct net_bridge *br)
  391. {
  392. }
  393. static inline void br_multicast_stop(struct net_bridge *br)
  394. {
  395. }
  396. static inline void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
  397. struct sk_buff *skb)
  398. {
  399. }
  400. static inline void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
  401. struct sk_buff *skb,
  402. struct sk_buff *skb2)
  403. {
  404. }
  405. static inline bool br_multicast_is_router(struct net_bridge *br)
  406. {
  407. return 0;
  408. }
  409. #endif
  410. /* br_netfilter.c */
  411. #ifdef CONFIG_BRIDGE_NETFILTER
  412. extern int br_netfilter_init(void);
  413. extern void br_netfilter_fini(void);
  414. extern void br_netfilter_rtable_init(struct net_bridge *);
  415. #else
  416. #define br_netfilter_init() (0)
  417. #define br_netfilter_fini() do { } while(0)
  418. #define br_netfilter_rtable_init(x)
  419. #endif
  420. /* br_stp.c */
  421. extern void br_log_state(const struct net_bridge_port *p);
  422. extern struct net_bridge_port *br_get_port(struct net_bridge *br,
  423. u16 port_no);
  424. extern void br_init_port(struct net_bridge_port *p);
  425. extern void br_become_designated_port(struct net_bridge_port *p);
  426. extern void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
  427. extern int br_set_forward_delay(struct net_bridge *br, unsigned long x);
  428. extern int br_set_hello_time(struct net_bridge *br, unsigned long x);
  429. extern int br_set_max_age(struct net_bridge *br, unsigned long x);
  430. /* br_stp_if.c */
  431. extern void br_stp_enable_bridge(struct net_bridge *br);
  432. extern void br_stp_disable_bridge(struct net_bridge *br);
  433. extern void br_stp_set_enabled(struct net_bridge *br, unsigned long val);
  434. extern void br_stp_enable_port(struct net_bridge_port *p);
  435. extern void br_stp_disable_port(struct net_bridge_port *p);
  436. extern bool br_stp_recalculate_bridge_id(struct net_bridge *br);
  437. extern void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
  438. extern void br_stp_set_bridge_priority(struct net_bridge *br,
  439. u16 newprio);
  440. extern int br_stp_set_port_priority(struct net_bridge_port *p,
  441. unsigned long newprio);
  442. extern int br_stp_set_path_cost(struct net_bridge_port *p,
  443. unsigned long path_cost);
  444. extern ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
  445. /* br_stp_bpdu.c */
  446. struct stp_proto;
  447. extern void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
  448. struct net_device *dev);
  449. /* br_stp_timer.c */
  450. extern void br_stp_timer_init(struct net_bridge *br);
  451. extern void br_stp_port_timer_init(struct net_bridge_port *p);
  452. extern unsigned long br_timer_value(const struct timer_list *timer);
  453. /* br.c */
  454. #if IS_ENABLED(CONFIG_ATM_LANE)
  455. extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
  456. #endif
  457. /* br_netlink.c */
  458. extern struct rtnl_link_ops br_link_ops;
  459. extern int br_netlink_init(void);
  460. extern void br_netlink_fini(void);
  461. extern void br_ifinfo_notify(int event, struct net_bridge_port *port);
  462. #ifdef CONFIG_SYSFS
  463. /* br_sysfs_if.c */
  464. extern const struct sysfs_ops brport_sysfs_ops;
  465. extern int br_sysfs_addif(struct net_bridge_port *p);
  466. extern int br_sysfs_renameif(struct net_bridge_port *p);
  467. /* br_sysfs_br.c */
  468. extern int br_sysfs_addbr(struct net_device *dev);
  469. extern void br_sysfs_delbr(struct net_device *dev);
  470. #else
  471. #define br_sysfs_addif(p) (0)
  472. #define br_sysfs_renameif(p) (0)
  473. #define br_sysfs_addbr(dev) (0)
  474. #define br_sysfs_delbr(dev) do { } while(0)
  475. #endif /* CONFIG_SYSFS */
  476. #endif