br_private.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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. #include <net/ip6_fib.h>
  20. #include <linux/if_vlan.h>
  21. #include <linux/rhashtable.h>
  22. #define BR_HASH_BITS 8
  23. #define BR_HASH_SIZE (1 << BR_HASH_BITS)
  24. #define BR_HOLD_TIME (1*HZ)
  25. #define BR_PORT_BITS 10
  26. #define BR_MAX_PORTS (1<<BR_PORT_BITS)
  27. #define BR_VERSION "2.3"
  28. /* Control of forwarding link local multicast */
  29. #define BR_GROUPFWD_DEFAULT 0
  30. /* Don't allow forwarding of control protocols like STP, MAC PAUSE and LACP */
  31. #define BR_GROUPFWD_RESTRICTED 0x0007u
  32. /* The Nearest Customer Bridge Group Address, 01-80-C2-00-00-[00,0B,0C,0D,0F] */
  33. #define BR_GROUPFWD_8021AD 0xB801u
  34. /* Path to usermode spanning tree program */
  35. #define BR_STP_PROG "/sbin/bridge-stp"
  36. typedef struct bridge_id bridge_id;
  37. typedef struct mac_addr mac_addr;
  38. typedef __u16 port_id;
  39. struct bridge_id
  40. {
  41. unsigned char prio[2];
  42. unsigned char addr[ETH_ALEN];
  43. };
  44. struct mac_addr
  45. {
  46. unsigned char addr[ETH_ALEN];
  47. };
  48. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  49. /* our own querier */
  50. struct bridge_mcast_own_query {
  51. struct timer_list timer;
  52. u32 startup_sent;
  53. };
  54. /* other querier */
  55. struct bridge_mcast_other_query {
  56. struct timer_list timer;
  57. unsigned long delay_time;
  58. };
  59. /* selected querier */
  60. struct bridge_mcast_querier {
  61. struct br_ip addr;
  62. struct net_bridge_port __rcu *port;
  63. };
  64. #endif
  65. /**
  66. * struct net_bridge_vlan - per-vlan entry
  67. *
  68. * @vnode: rhashtable member
  69. * @vid: VLAN id
  70. * @flags: bridge vlan flags
  71. * @br: if MASTER flag set, this points to a bridge struct
  72. * @port: if MASTER flag unset, this points to a port struct
  73. * @refcnt: if MASTER flag set, this is bumped for each port referencing it
  74. * @brvlan: if MASTER flag unset, this points to the global per-VLAN context
  75. * for this VLAN entry
  76. * @vlist: sorted list of VLAN entries
  77. * @rcu: used for entry destruction
  78. *
  79. * This structure is shared between the global per-VLAN entries contained in
  80. * the bridge rhashtable and the local per-port per-VLAN entries contained in
  81. * the port's rhashtable. The union entries should be interpreted depending on
  82. * the entry flags that are set.
  83. */
  84. struct net_bridge_vlan {
  85. struct rhash_head vnode;
  86. u16 vid;
  87. u16 flags;
  88. union {
  89. struct net_bridge *br;
  90. struct net_bridge_port *port;
  91. };
  92. union {
  93. atomic_t refcnt;
  94. struct net_bridge_vlan *brvlan;
  95. };
  96. struct list_head vlist;
  97. struct rcu_head rcu;
  98. };
  99. /**
  100. * struct net_bridge_vlan_group
  101. *
  102. * @vlan_hash: VLAN entry rhashtable
  103. * @vlan_list: sorted VLAN entry list
  104. * @num_vlans: number of total VLAN entries
  105. * @pvid: PVID VLAN id
  106. *
  107. * IMPORTANT: Be careful when checking if there're VLAN entries using list
  108. * primitives because the bridge can have entries in its list which
  109. * are just for global context but not for filtering, i.e. they have
  110. * the master flag set but not the brentry flag. If you have to check
  111. * if there're "real" entries in the bridge please test @num_vlans
  112. */
  113. struct net_bridge_vlan_group {
  114. struct rhashtable vlan_hash;
  115. struct list_head vlan_list;
  116. u16 num_vlans;
  117. u16 pvid;
  118. };
  119. struct net_bridge_fdb_entry
  120. {
  121. struct hlist_node hlist;
  122. struct net_bridge_port *dst;
  123. unsigned long updated;
  124. unsigned long used;
  125. mac_addr addr;
  126. __u16 vlan_id;
  127. unsigned char is_local:1,
  128. is_static:1,
  129. added_by_user:1,
  130. added_by_external_learn:1;
  131. struct rcu_head rcu;
  132. };
  133. struct net_bridge_port_group {
  134. struct net_bridge_port *port;
  135. struct net_bridge_port_group __rcu *next;
  136. struct hlist_node mglist;
  137. struct rcu_head rcu;
  138. struct timer_list timer;
  139. struct br_ip addr;
  140. unsigned char state;
  141. };
  142. struct net_bridge_mdb_entry
  143. {
  144. struct hlist_node hlist[2];
  145. struct net_bridge *br;
  146. struct net_bridge_port_group __rcu *ports;
  147. struct rcu_head rcu;
  148. struct timer_list timer;
  149. struct br_ip addr;
  150. bool mglist;
  151. };
  152. struct net_bridge_mdb_htable
  153. {
  154. struct hlist_head *mhash;
  155. struct rcu_head rcu;
  156. struct net_bridge_mdb_htable *old;
  157. u32 size;
  158. u32 max;
  159. u32 secret;
  160. u32 ver;
  161. };
  162. struct net_bridge_port
  163. {
  164. struct net_bridge *br;
  165. struct net_device *dev;
  166. struct list_head list;
  167. /* STP */
  168. u8 priority;
  169. u8 state;
  170. u16 port_no;
  171. unsigned char topology_change_ack;
  172. unsigned char config_pending;
  173. port_id port_id;
  174. port_id designated_port;
  175. bridge_id designated_root;
  176. bridge_id designated_bridge;
  177. u32 path_cost;
  178. u32 designated_cost;
  179. unsigned long designated_age;
  180. struct timer_list forward_delay_timer;
  181. struct timer_list hold_timer;
  182. struct timer_list message_age_timer;
  183. struct kobject kobj;
  184. struct rcu_head rcu;
  185. unsigned long flags;
  186. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  187. struct bridge_mcast_own_query ip4_own_query;
  188. #if IS_ENABLED(CONFIG_IPV6)
  189. struct bridge_mcast_own_query ip6_own_query;
  190. #endif /* IS_ENABLED(CONFIG_IPV6) */
  191. unsigned char multicast_router;
  192. struct timer_list multicast_router_timer;
  193. struct hlist_head mglist;
  194. struct hlist_node rlist;
  195. #endif
  196. #ifdef CONFIG_SYSFS
  197. char sysfs_name[IFNAMSIZ];
  198. #endif
  199. #ifdef CONFIG_NET_POLL_CONTROLLER
  200. struct netpoll *np;
  201. #endif
  202. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  203. struct net_bridge_vlan_group __rcu *vlgrp;
  204. #endif
  205. };
  206. #define br_auto_port(p) ((p)->flags & BR_AUTO_MASK)
  207. #define br_promisc_port(p) ((p)->flags & BR_PROMISC)
  208. #define br_port_exists(dev) (dev->priv_flags & IFF_BRIDGE_PORT)
  209. static inline struct net_bridge_port *br_port_get_rcu(const struct net_device *dev)
  210. {
  211. return rcu_dereference(dev->rx_handler_data);
  212. }
  213. static inline struct net_bridge_port *br_port_get_rtnl(const struct net_device *dev)
  214. {
  215. return br_port_exists(dev) ?
  216. rtnl_dereference(dev->rx_handler_data) : NULL;
  217. }
  218. struct net_bridge
  219. {
  220. spinlock_t lock;
  221. struct list_head port_list;
  222. struct net_device *dev;
  223. struct pcpu_sw_netstats __percpu *stats;
  224. spinlock_t hash_lock;
  225. struct hlist_head hash[BR_HASH_SIZE];
  226. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  227. union {
  228. struct rtable fake_rtable;
  229. struct rt6_info fake_rt6_info;
  230. };
  231. bool nf_call_iptables;
  232. bool nf_call_ip6tables;
  233. bool nf_call_arptables;
  234. #endif
  235. u16 group_fwd_mask;
  236. u16 group_fwd_mask_required;
  237. /* STP */
  238. bridge_id designated_root;
  239. bridge_id bridge_id;
  240. u32 root_path_cost;
  241. unsigned long max_age;
  242. unsigned long hello_time;
  243. unsigned long forward_delay;
  244. unsigned long bridge_max_age;
  245. unsigned long ageing_time;
  246. unsigned long bridge_hello_time;
  247. unsigned long bridge_forward_delay;
  248. u8 group_addr[ETH_ALEN];
  249. bool group_addr_set;
  250. u16 root_port;
  251. enum {
  252. BR_NO_STP, /* no spanning tree */
  253. BR_KERNEL_STP, /* old STP in kernel */
  254. BR_USER_STP, /* new RSTP in userspace */
  255. } stp_enabled;
  256. unsigned char topology_change;
  257. unsigned char topology_change_detected;
  258. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  259. unsigned char multicast_router;
  260. u8 multicast_disabled:1;
  261. u8 multicast_querier:1;
  262. u8 multicast_query_use_ifaddr:1;
  263. u32 hash_elasticity;
  264. u32 hash_max;
  265. u32 multicast_last_member_count;
  266. u32 multicast_startup_query_count;
  267. unsigned long multicast_last_member_interval;
  268. unsigned long multicast_membership_interval;
  269. unsigned long multicast_querier_interval;
  270. unsigned long multicast_query_interval;
  271. unsigned long multicast_query_response_interval;
  272. unsigned long multicast_startup_query_interval;
  273. spinlock_t multicast_lock;
  274. struct net_bridge_mdb_htable __rcu *mdb;
  275. struct hlist_head router_list;
  276. struct timer_list multicast_router_timer;
  277. struct bridge_mcast_other_query ip4_other_query;
  278. struct bridge_mcast_own_query ip4_own_query;
  279. struct bridge_mcast_querier ip4_querier;
  280. #if IS_ENABLED(CONFIG_IPV6)
  281. struct bridge_mcast_other_query ip6_other_query;
  282. struct bridge_mcast_own_query ip6_own_query;
  283. struct bridge_mcast_querier ip6_querier;
  284. #endif /* IS_ENABLED(CONFIG_IPV6) */
  285. #endif
  286. struct timer_list hello_timer;
  287. struct timer_list tcn_timer;
  288. struct timer_list topology_change_timer;
  289. struct timer_list gc_timer;
  290. struct kobject *ifobj;
  291. u32 auto_cnt;
  292. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  293. struct net_bridge_vlan_group __rcu *vlgrp;
  294. u8 vlan_enabled;
  295. __be16 vlan_proto;
  296. u16 default_pvid;
  297. #endif
  298. };
  299. struct br_input_skb_cb {
  300. struct net_device *brdev;
  301. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  302. int igmp;
  303. int mrouters_only;
  304. #endif
  305. bool proxyarp_replied;
  306. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  307. bool vlan_filtered;
  308. #endif
  309. };
  310. #define BR_INPUT_SKB_CB(__skb) ((struct br_input_skb_cb *)(__skb)->cb)
  311. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  312. # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (BR_INPUT_SKB_CB(__skb)->mrouters_only)
  313. #else
  314. # define BR_INPUT_SKB_CB_MROUTERS_ONLY(__skb) (0)
  315. #endif
  316. #define br_printk(level, br, format, args...) \
  317. printk(level "%s: " format, (br)->dev->name, ##args)
  318. #define br_err(__br, format, args...) \
  319. br_printk(KERN_ERR, __br, format, ##args)
  320. #define br_warn(__br, format, args...) \
  321. br_printk(KERN_WARNING, __br, format, ##args)
  322. #define br_notice(__br, format, args...) \
  323. br_printk(KERN_NOTICE, __br, format, ##args)
  324. #define br_info(__br, format, args...) \
  325. br_printk(KERN_INFO, __br, format, ##args)
  326. #define br_debug(br, format, args...) \
  327. pr_debug("%s: " format, (br)->dev->name, ##args)
  328. /* called under bridge lock */
  329. static inline int br_is_root_bridge(const struct net_bridge *br)
  330. {
  331. return !memcmp(&br->bridge_id, &br->designated_root, 8);
  332. }
  333. /* check if a VLAN entry is global */
  334. static inline bool br_vlan_is_master(const struct net_bridge_vlan *v)
  335. {
  336. return v->flags & BRIDGE_VLAN_INFO_MASTER;
  337. }
  338. /* check if a VLAN entry is used by the bridge */
  339. static inline bool br_vlan_is_brentry(const struct net_bridge_vlan *v)
  340. {
  341. return v->flags & BRIDGE_VLAN_INFO_BRENTRY;
  342. }
  343. /* check if we should use the vlan entry, returns false if it's only context */
  344. static inline bool br_vlan_should_use(const struct net_bridge_vlan *v)
  345. {
  346. if (br_vlan_is_master(v)) {
  347. if (br_vlan_is_brentry(v))
  348. return true;
  349. else
  350. return false;
  351. }
  352. return true;
  353. }
  354. /* br_device.c */
  355. void br_dev_setup(struct net_device *dev);
  356. void br_dev_delete(struct net_device *dev, struct list_head *list);
  357. netdev_tx_t br_dev_xmit(struct sk_buff *skb, struct net_device *dev);
  358. #ifdef CONFIG_NET_POLL_CONTROLLER
  359. static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
  360. struct sk_buff *skb)
  361. {
  362. struct netpoll *np = p->np;
  363. if (np)
  364. netpoll_send_skb(np, skb);
  365. }
  366. int br_netpoll_enable(struct net_bridge_port *p);
  367. void br_netpoll_disable(struct net_bridge_port *p);
  368. #else
  369. static inline void br_netpoll_send_skb(const struct net_bridge_port *p,
  370. struct sk_buff *skb)
  371. {
  372. }
  373. static inline int br_netpoll_enable(struct net_bridge_port *p)
  374. {
  375. return 0;
  376. }
  377. static inline void br_netpoll_disable(struct net_bridge_port *p)
  378. {
  379. }
  380. #endif
  381. /* br_fdb.c */
  382. int br_fdb_init(void);
  383. void br_fdb_fini(void);
  384. void br_fdb_flush(struct net_bridge *br);
  385. void br_fdb_find_delete_local(struct net_bridge *br,
  386. const struct net_bridge_port *p,
  387. const unsigned char *addr, u16 vid);
  388. void br_fdb_changeaddr(struct net_bridge_port *p, const unsigned char *newaddr);
  389. void br_fdb_change_mac_address(struct net_bridge *br, const u8 *newaddr);
  390. void br_fdb_cleanup(unsigned long arg);
  391. void br_fdb_delete_by_port(struct net_bridge *br,
  392. const struct net_bridge_port *p, u16 vid, int do_all);
  393. struct net_bridge_fdb_entry *__br_fdb_get(struct net_bridge *br,
  394. const unsigned char *addr, __u16 vid);
  395. int br_fdb_test_addr(struct net_device *dev, unsigned char *addr);
  396. int br_fdb_fillbuf(struct net_bridge *br, void *buf, unsigned long count,
  397. unsigned long off);
  398. int br_fdb_insert(struct net_bridge *br, struct net_bridge_port *source,
  399. const unsigned char *addr, u16 vid);
  400. void br_fdb_update(struct net_bridge *br, struct net_bridge_port *source,
  401. const unsigned char *addr, u16 vid, bool added_by_user);
  402. int br_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
  403. struct net_device *dev, const unsigned char *addr, u16 vid);
  404. int br_fdb_add(struct ndmsg *nlh, struct nlattr *tb[], struct net_device *dev,
  405. const unsigned char *addr, u16 vid, u16 nlh_flags);
  406. int br_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
  407. struct net_device *dev, struct net_device *fdev, int idx);
  408. int br_fdb_sync_static(struct net_bridge *br, struct net_bridge_port *p);
  409. void br_fdb_unsync_static(struct net_bridge *br, struct net_bridge_port *p);
  410. int br_fdb_external_learn_add(struct net_bridge *br, struct net_bridge_port *p,
  411. const unsigned char *addr, u16 vid);
  412. int br_fdb_external_learn_del(struct net_bridge *br, struct net_bridge_port *p,
  413. const unsigned char *addr, u16 vid);
  414. /* br_forward.c */
  415. void br_deliver(const struct net_bridge_port *to, struct sk_buff *skb);
  416. int br_dev_queue_push_xmit(struct net *net, struct sock *sk, struct sk_buff *skb);
  417. void br_forward(const struct net_bridge_port *to,
  418. struct sk_buff *skb, struct sk_buff *skb0);
  419. int br_forward_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
  420. void br_flood_deliver(struct net_bridge *br, struct sk_buff *skb, bool unicast);
  421. void br_flood_forward(struct net_bridge *br, struct sk_buff *skb,
  422. struct sk_buff *skb2, bool unicast);
  423. /* br_if.c */
  424. void br_port_carrier_check(struct net_bridge_port *p);
  425. int br_add_bridge(struct net *net, const char *name);
  426. int br_del_bridge(struct net *net, const char *name);
  427. int br_add_if(struct net_bridge *br, struct net_device *dev);
  428. int br_del_if(struct net_bridge *br, struct net_device *dev);
  429. int br_min_mtu(const struct net_bridge *br);
  430. netdev_features_t br_features_recompute(struct net_bridge *br,
  431. netdev_features_t features);
  432. void br_port_flags_change(struct net_bridge_port *port, unsigned long mask);
  433. void br_manage_promisc(struct net_bridge *br);
  434. /* br_input.c */
  435. int br_handle_frame_finish(struct net *net, struct sock *sk, struct sk_buff *skb);
  436. rx_handler_result_t br_handle_frame(struct sk_buff **pskb);
  437. static inline bool br_rx_handler_check_rcu(const struct net_device *dev)
  438. {
  439. return rcu_dereference(dev->rx_handler) == br_handle_frame;
  440. }
  441. static inline struct net_bridge_port *br_port_get_check_rcu(const struct net_device *dev)
  442. {
  443. return br_rx_handler_check_rcu(dev) ? br_port_get_rcu(dev) : NULL;
  444. }
  445. /* br_ioctl.c */
  446. int br_dev_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
  447. int br_ioctl_deviceless_stub(struct net *net, unsigned int cmd,
  448. void __user *arg);
  449. /* br_multicast.c */
  450. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  451. extern unsigned int br_mdb_rehash_seq;
  452. int br_multicast_rcv(struct net_bridge *br, struct net_bridge_port *port,
  453. struct sk_buff *skb, u16 vid);
  454. struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
  455. struct sk_buff *skb, u16 vid);
  456. void br_multicast_add_port(struct net_bridge_port *port);
  457. void br_multicast_del_port(struct net_bridge_port *port);
  458. void br_multicast_enable_port(struct net_bridge_port *port);
  459. void br_multicast_disable_port(struct net_bridge_port *port);
  460. void br_multicast_init(struct net_bridge *br);
  461. void br_multicast_open(struct net_bridge *br);
  462. void br_multicast_stop(struct net_bridge *br);
  463. void br_multicast_dev_del(struct net_bridge *br);
  464. void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
  465. struct sk_buff *skb);
  466. void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
  467. struct sk_buff *skb, struct sk_buff *skb2);
  468. int br_multicast_set_router(struct net_bridge *br, unsigned long val);
  469. int br_multicast_set_port_router(struct net_bridge_port *p, unsigned long val);
  470. int br_multicast_toggle(struct net_bridge *br, unsigned long val);
  471. int br_multicast_set_querier(struct net_bridge *br, unsigned long val);
  472. int br_multicast_set_hash_max(struct net_bridge *br, unsigned long val);
  473. struct net_bridge_mdb_entry *
  474. br_mdb_ip_get(struct net_bridge_mdb_htable *mdb, struct br_ip *dst);
  475. struct net_bridge_mdb_entry *
  476. br_multicast_new_group(struct net_bridge *br, struct net_bridge_port *port,
  477. struct br_ip *group);
  478. void br_multicast_free_pg(struct rcu_head *head);
  479. struct net_bridge_port_group *
  480. br_multicast_new_port_group(struct net_bridge_port *port, struct br_ip *group,
  481. struct net_bridge_port_group __rcu *next,
  482. unsigned char state);
  483. void br_mdb_init(void);
  484. void br_mdb_uninit(void);
  485. void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
  486. struct br_ip *group, int type, u8 state);
  487. void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
  488. int type);
  489. #define mlock_dereference(X, br) \
  490. rcu_dereference_protected(X, lockdep_is_held(&br->multicast_lock))
  491. static inline bool br_multicast_is_router(struct net_bridge *br)
  492. {
  493. return br->multicast_router == 2 ||
  494. (br->multicast_router == 1 &&
  495. timer_pending(&br->multicast_router_timer));
  496. }
  497. static inline bool
  498. __br_multicast_querier_exists(struct net_bridge *br,
  499. struct bridge_mcast_other_query *querier)
  500. {
  501. return time_is_before_jiffies(querier->delay_time) &&
  502. (br->multicast_querier || timer_pending(&querier->timer));
  503. }
  504. static inline bool br_multicast_querier_exists(struct net_bridge *br,
  505. struct ethhdr *eth)
  506. {
  507. switch (eth->h_proto) {
  508. case (htons(ETH_P_IP)):
  509. return __br_multicast_querier_exists(br, &br->ip4_other_query);
  510. #if IS_ENABLED(CONFIG_IPV6)
  511. case (htons(ETH_P_IPV6)):
  512. return __br_multicast_querier_exists(br, &br->ip6_other_query);
  513. #endif
  514. default:
  515. return false;
  516. }
  517. }
  518. #else
  519. static inline int br_multicast_rcv(struct net_bridge *br,
  520. struct net_bridge_port *port,
  521. struct sk_buff *skb,
  522. u16 vid)
  523. {
  524. return 0;
  525. }
  526. static inline struct net_bridge_mdb_entry *br_mdb_get(struct net_bridge *br,
  527. struct sk_buff *skb, u16 vid)
  528. {
  529. return NULL;
  530. }
  531. static inline void br_multicast_add_port(struct net_bridge_port *port)
  532. {
  533. }
  534. static inline void br_multicast_del_port(struct net_bridge_port *port)
  535. {
  536. }
  537. static inline void br_multicast_enable_port(struct net_bridge_port *port)
  538. {
  539. }
  540. static inline void br_multicast_disable_port(struct net_bridge_port *port)
  541. {
  542. }
  543. static inline void br_multicast_init(struct net_bridge *br)
  544. {
  545. }
  546. static inline void br_multicast_open(struct net_bridge *br)
  547. {
  548. }
  549. static inline void br_multicast_stop(struct net_bridge *br)
  550. {
  551. }
  552. static inline void br_multicast_dev_del(struct net_bridge *br)
  553. {
  554. }
  555. static inline void br_multicast_deliver(struct net_bridge_mdb_entry *mdst,
  556. struct sk_buff *skb)
  557. {
  558. }
  559. static inline void br_multicast_forward(struct net_bridge_mdb_entry *mdst,
  560. struct sk_buff *skb,
  561. struct sk_buff *skb2)
  562. {
  563. }
  564. static inline bool br_multicast_is_router(struct net_bridge *br)
  565. {
  566. return 0;
  567. }
  568. static inline bool br_multicast_querier_exists(struct net_bridge *br,
  569. struct ethhdr *eth)
  570. {
  571. return false;
  572. }
  573. static inline void br_mdb_init(void)
  574. {
  575. }
  576. static inline void br_mdb_uninit(void)
  577. {
  578. }
  579. #endif
  580. /* br_vlan.c */
  581. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  582. bool br_allowed_ingress(const struct net_bridge *br,
  583. struct net_bridge_vlan_group *vg, struct sk_buff *skb,
  584. u16 *vid);
  585. bool br_allowed_egress(struct net_bridge_vlan_group *vg,
  586. const struct sk_buff *skb);
  587. bool br_should_learn(struct net_bridge_port *p, struct sk_buff *skb, u16 *vid);
  588. struct sk_buff *br_handle_vlan(struct net_bridge *br,
  589. struct net_bridge_vlan_group *vg,
  590. struct sk_buff *skb);
  591. int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags);
  592. int br_vlan_delete(struct net_bridge *br, u16 vid);
  593. void br_vlan_flush(struct net_bridge *br);
  594. struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg, u16 vid);
  595. void br_recalculate_fwd_mask(struct net_bridge *br);
  596. int __br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
  597. int br_vlan_filter_toggle(struct net_bridge *br, unsigned long val);
  598. int __br_vlan_set_proto(struct net_bridge *br, __be16 proto);
  599. int br_vlan_set_proto(struct net_bridge *br, unsigned long val);
  600. int br_vlan_init(struct net_bridge *br);
  601. int br_vlan_set_default_pvid(struct net_bridge *br, unsigned long val);
  602. int __br_vlan_set_default_pvid(struct net_bridge *br, u16 pvid);
  603. int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags);
  604. int nbp_vlan_delete(struct net_bridge_port *port, u16 vid);
  605. void nbp_vlan_flush(struct net_bridge_port *port);
  606. int nbp_vlan_init(struct net_bridge_port *port);
  607. int nbp_get_num_vlan_infos(struct net_bridge_port *p, u32 filter_mask);
  608. static inline struct net_bridge_vlan_group *br_vlan_group(
  609. const struct net_bridge *br)
  610. {
  611. return rtnl_dereference(br->vlgrp);
  612. }
  613. static inline struct net_bridge_vlan_group *nbp_vlan_group(
  614. const struct net_bridge_port *p)
  615. {
  616. return rtnl_dereference(p->vlgrp);
  617. }
  618. static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
  619. const struct net_bridge *br)
  620. {
  621. return rcu_dereference(br->vlgrp);
  622. }
  623. static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
  624. const struct net_bridge_port *p)
  625. {
  626. return rcu_dereference(p->vlgrp);
  627. }
  628. /* Since bridge now depends on 8021Q module, but the time bridge sees the
  629. * skb, the vlan tag will always be present if the frame was tagged.
  630. */
  631. static inline int br_vlan_get_tag(const struct sk_buff *skb, u16 *vid)
  632. {
  633. int err = 0;
  634. if (skb_vlan_tag_present(skb)) {
  635. *vid = skb_vlan_tag_get(skb) & VLAN_VID_MASK;
  636. } else {
  637. *vid = 0;
  638. err = -EINVAL;
  639. }
  640. return err;
  641. }
  642. static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
  643. {
  644. if (!vg)
  645. return 0;
  646. smp_rmb();
  647. return vg->pvid;
  648. }
  649. static inline int br_vlan_enabled(struct net_bridge *br)
  650. {
  651. return br->vlan_enabled;
  652. }
  653. #else
  654. static inline bool br_allowed_ingress(const struct net_bridge *br,
  655. struct net_bridge_vlan_group *vg,
  656. struct sk_buff *skb,
  657. u16 *vid)
  658. {
  659. return true;
  660. }
  661. static inline bool br_allowed_egress(struct net_bridge_vlan_group *vg,
  662. const struct sk_buff *skb)
  663. {
  664. return true;
  665. }
  666. static inline bool br_should_learn(struct net_bridge_port *p,
  667. struct sk_buff *skb, u16 *vid)
  668. {
  669. return true;
  670. }
  671. static inline struct sk_buff *br_handle_vlan(struct net_bridge *br,
  672. struct net_bridge_vlan_group *vg,
  673. struct sk_buff *skb)
  674. {
  675. return skb;
  676. }
  677. static inline int br_vlan_add(struct net_bridge *br, u16 vid, u16 flags)
  678. {
  679. return -EOPNOTSUPP;
  680. }
  681. static inline int br_vlan_delete(struct net_bridge *br, u16 vid)
  682. {
  683. return -EOPNOTSUPP;
  684. }
  685. static inline void br_vlan_flush(struct net_bridge *br)
  686. {
  687. }
  688. static inline void br_recalculate_fwd_mask(struct net_bridge *br)
  689. {
  690. }
  691. static inline int br_vlan_init(struct net_bridge *br)
  692. {
  693. return 0;
  694. }
  695. static inline int nbp_vlan_add(struct net_bridge_port *port, u16 vid, u16 flags)
  696. {
  697. return -EOPNOTSUPP;
  698. }
  699. static inline int nbp_vlan_delete(struct net_bridge_port *port, u16 vid)
  700. {
  701. return -EOPNOTSUPP;
  702. }
  703. static inline void nbp_vlan_flush(struct net_bridge_port *port)
  704. {
  705. }
  706. static inline struct net_bridge_vlan *br_vlan_find(struct net_bridge_vlan_group *vg,
  707. u16 vid)
  708. {
  709. return NULL;
  710. }
  711. static inline int nbp_vlan_init(struct net_bridge_port *port)
  712. {
  713. return 0;
  714. }
  715. static inline u16 br_vlan_get_tag(const struct sk_buff *skb, u16 *tag)
  716. {
  717. return 0;
  718. }
  719. static inline u16 br_get_pvid(const struct net_bridge_vlan_group *vg)
  720. {
  721. return 0;
  722. }
  723. static inline int br_vlan_enabled(struct net_bridge *br)
  724. {
  725. return 0;
  726. }
  727. static inline int __br_vlan_filter_toggle(struct net_bridge *br,
  728. unsigned long val)
  729. {
  730. return -EOPNOTSUPP;
  731. }
  732. static inline int nbp_get_num_vlan_infos(struct net_bridge_port *p,
  733. u32 filter_mask)
  734. {
  735. return 0;
  736. }
  737. static inline struct net_bridge_vlan_group *br_vlan_group(
  738. const struct net_bridge *br)
  739. {
  740. return NULL;
  741. }
  742. static inline struct net_bridge_vlan_group *nbp_vlan_group(
  743. const struct net_bridge_port *p)
  744. {
  745. return NULL;
  746. }
  747. static inline struct net_bridge_vlan_group *br_vlan_group_rcu(
  748. const struct net_bridge *br)
  749. {
  750. return NULL;
  751. }
  752. static inline struct net_bridge_vlan_group *nbp_vlan_group_rcu(
  753. const struct net_bridge_port *p)
  754. {
  755. return NULL;
  756. }
  757. #endif
  758. struct nf_br_ops {
  759. int (*br_dev_xmit_hook)(struct sk_buff *skb);
  760. };
  761. extern const struct nf_br_ops __rcu *nf_br_ops;
  762. /* br_netfilter.c */
  763. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  764. int br_nf_core_init(void);
  765. void br_nf_core_fini(void);
  766. void br_netfilter_rtable_init(struct net_bridge *);
  767. #else
  768. static inline int br_nf_core_init(void) { return 0; }
  769. static inline void br_nf_core_fini(void) {}
  770. #define br_netfilter_rtable_init(x)
  771. #endif
  772. /* br_stp.c */
  773. void br_log_state(const struct net_bridge_port *p);
  774. void br_set_state(struct net_bridge_port *p, unsigned int state);
  775. struct net_bridge_port *br_get_port(struct net_bridge *br, u16 port_no);
  776. void br_init_port(struct net_bridge_port *p);
  777. void br_become_designated_port(struct net_bridge_port *p);
  778. void __br_set_forward_delay(struct net_bridge *br, unsigned long t);
  779. int br_set_forward_delay(struct net_bridge *br, unsigned long x);
  780. int br_set_hello_time(struct net_bridge *br, unsigned long x);
  781. int br_set_max_age(struct net_bridge *br, unsigned long x);
  782. int br_set_ageing_time(struct net_bridge *br, u32 ageing_time);
  783. /* br_stp_if.c */
  784. void br_stp_enable_bridge(struct net_bridge *br);
  785. void br_stp_disable_bridge(struct net_bridge *br);
  786. void br_stp_set_enabled(struct net_bridge *br, unsigned long val);
  787. void br_stp_enable_port(struct net_bridge_port *p);
  788. void br_stp_disable_port(struct net_bridge_port *p);
  789. bool br_stp_recalculate_bridge_id(struct net_bridge *br);
  790. void br_stp_change_bridge_id(struct net_bridge *br, const unsigned char *a);
  791. void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio);
  792. int br_stp_set_port_priority(struct net_bridge_port *p, unsigned long newprio);
  793. int br_stp_set_path_cost(struct net_bridge_port *p, unsigned long path_cost);
  794. ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id);
  795. /* br_stp_bpdu.c */
  796. struct stp_proto;
  797. void br_stp_rcv(const struct stp_proto *proto, struct sk_buff *skb,
  798. struct net_device *dev);
  799. /* br_stp_timer.c */
  800. void br_stp_timer_init(struct net_bridge *br);
  801. void br_stp_port_timer_init(struct net_bridge_port *p);
  802. unsigned long br_timer_value(const struct timer_list *timer);
  803. /* br.c */
  804. #if IS_ENABLED(CONFIG_ATM_LANE)
  805. extern int (*br_fdb_test_addr_hook)(struct net_device *dev, unsigned char *addr);
  806. #endif
  807. /* br_netlink.c */
  808. extern struct rtnl_link_ops br_link_ops;
  809. int br_netlink_init(void);
  810. void br_netlink_fini(void);
  811. void br_ifinfo_notify(int event, struct net_bridge_port *port);
  812. int br_setlink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
  813. int br_dellink(struct net_device *dev, struct nlmsghdr *nlmsg, u16 flags);
  814. int br_getlink(struct sk_buff *skb, u32 pid, u32 seq, struct net_device *dev,
  815. u32 filter_mask, int nlflags);
  816. #ifdef CONFIG_SYSFS
  817. /* br_sysfs_if.c */
  818. extern const struct sysfs_ops brport_sysfs_ops;
  819. int br_sysfs_addif(struct net_bridge_port *p);
  820. int br_sysfs_renameif(struct net_bridge_port *p);
  821. /* br_sysfs_br.c */
  822. int br_sysfs_addbr(struct net_device *dev);
  823. void br_sysfs_delbr(struct net_device *dev);
  824. #else
  825. static inline int br_sysfs_addif(struct net_bridge_port *p) { return 0; }
  826. static inline int br_sysfs_renameif(struct net_bridge_port *p) { return 0; }
  827. static inline int br_sysfs_addbr(struct net_device *dev) { return 0; }
  828. static inline void br_sysfs_delbr(struct net_device *dev) { return; }
  829. #endif /* CONFIG_SYSFS */
  830. #endif