br_netlink.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  1. /*
  2. * Bridge netlink control interface
  3. *
  4. * Authors:
  5. * Stephen Hemminger <shemminger@osdl.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. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/etherdevice.h>
  15. #include <net/rtnetlink.h>
  16. #include <net/net_namespace.h>
  17. #include <net/sock.h>
  18. #include <uapi/linux/if_bridge.h>
  19. #include "br_private.h"
  20. #include "br_private_stp.h"
  21. static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
  22. u32 filter_mask)
  23. {
  24. struct net_bridge_vlan *v;
  25. u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
  26. u16 flags, pvid;
  27. int num_vlans = 0;
  28. if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
  29. return 0;
  30. pvid = br_get_pvid(vg);
  31. /* Count number of vlan infos */
  32. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  33. flags = 0;
  34. /* only a context, bridge vlan not activated */
  35. if (!br_vlan_should_use(v))
  36. continue;
  37. if (v->vid == pvid)
  38. flags |= BRIDGE_VLAN_INFO_PVID;
  39. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  40. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  41. if (vid_range_start == 0) {
  42. goto initvars;
  43. } else if ((v->vid - vid_range_end) == 1 &&
  44. flags == vid_range_flags) {
  45. vid_range_end = v->vid;
  46. continue;
  47. } else {
  48. if ((vid_range_end - vid_range_start) > 0)
  49. num_vlans += 2;
  50. else
  51. num_vlans += 1;
  52. }
  53. initvars:
  54. vid_range_start = v->vid;
  55. vid_range_end = v->vid;
  56. vid_range_flags = flags;
  57. }
  58. if (vid_range_start != 0) {
  59. if ((vid_range_end - vid_range_start) > 0)
  60. num_vlans += 2;
  61. else
  62. num_vlans += 1;
  63. }
  64. return num_vlans;
  65. }
  66. static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
  67. u32 filter_mask)
  68. {
  69. int num_vlans;
  70. if (!vg)
  71. return 0;
  72. if (filter_mask & RTEXT_FILTER_BRVLAN)
  73. return vg->num_vlans;
  74. rcu_read_lock();
  75. num_vlans = __get_num_vlan_infos(vg, filter_mask);
  76. rcu_read_unlock();
  77. return num_vlans;
  78. }
  79. static size_t br_get_link_af_size_filtered(const struct net_device *dev,
  80. u32 filter_mask)
  81. {
  82. struct net_bridge_vlan_group *vg = NULL;
  83. struct net_bridge_port *p;
  84. struct net_bridge *br;
  85. int num_vlan_infos;
  86. rcu_read_lock();
  87. if (br_port_exists(dev)) {
  88. p = br_port_get_rcu(dev);
  89. vg = nbp_vlan_group_rcu(p);
  90. } else if (dev->priv_flags & IFF_EBRIDGE) {
  91. br = netdev_priv(dev);
  92. vg = br_vlan_group_rcu(br);
  93. }
  94. num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
  95. rcu_read_unlock();
  96. /* Each VLAN is returned in bridge_vlan_info along with flags */
  97. return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
  98. }
  99. static inline size_t br_port_info_size(void)
  100. {
  101. return nla_total_size(1) /* IFLA_BRPORT_STATE */
  102. + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
  103. + nla_total_size(4) /* IFLA_BRPORT_COST */
  104. + nla_total_size(1) /* IFLA_BRPORT_MODE */
  105. + nla_total_size(1) /* IFLA_BRPORT_GUARD */
  106. + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
  107. + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
  108. + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
  109. + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
  110. + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
  111. + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
  112. + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
  113. + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
  114. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
  115. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
  116. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
  117. + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
  118. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
  119. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
  120. + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
  121. + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
  122. + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
  123. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  124. + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
  125. #endif
  126. + 0;
  127. }
  128. static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
  129. {
  130. return NLMSG_ALIGN(sizeof(struct ifinfomsg))
  131. + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
  132. + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
  133. + nla_total_size(4) /* IFLA_MASTER */
  134. + nla_total_size(4) /* IFLA_MTU */
  135. + nla_total_size(4) /* IFLA_LINK */
  136. + nla_total_size(1) /* IFLA_OPERSTATE */
  137. + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
  138. + nla_total_size(br_get_link_af_size_filtered(dev,
  139. filter_mask)); /* IFLA_AF_SPEC */
  140. }
  141. static int br_port_fill_attrs(struct sk_buff *skb,
  142. const struct net_bridge_port *p)
  143. {
  144. u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
  145. u64 timerval;
  146. if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
  147. nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
  148. nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
  149. nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
  150. nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
  151. nla_put_u8(skb, IFLA_BRPORT_PROTECT,
  152. !!(p->flags & BR_ROOT_BLOCK)) ||
  153. nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
  154. !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
  155. nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
  156. nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
  157. !!(p->flags & BR_FLOOD)) ||
  158. nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
  159. !!(p->flags & BR_MCAST_FLOOD)) ||
  160. nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
  161. nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
  162. !!(p->flags & BR_PROXYARP_WIFI)) ||
  163. nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
  164. &p->designated_root) ||
  165. nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
  166. &p->designated_bridge) ||
  167. nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
  168. nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
  169. nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
  170. nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
  171. nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
  172. p->topology_change_ack) ||
  173. nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending))
  174. return -EMSGSIZE;
  175. timerval = br_timer_value(&p->message_age_timer);
  176. if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
  177. IFLA_BRPORT_PAD))
  178. return -EMSGSIZE;
  179. timerval = br_timer_value(&p->forward_delay_timer);
  180. if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
  181. IFLA_BRPORT_PAD))
  182. return -EMSGSIZE;
  183. timerval = br_timer_value(&p->hold_timer);
  184. if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
  185. IFLA_BRPORT_PAD))
  186. return -EMSGSIZE;
  187. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  188. if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
  189. p->multicast_router))
  190. return -EMSGSIZE;
  191. #endif
  192. return 0;
  193. }
  194. static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
  195. u16 vid_end, u16 flags)
  196. {
  197. struct bridge_vlan_info vinfo;
  198. if ((vid_end - vid_start) > 0) {
  199. /* add range to skb */
  200. vinfo.vid = vid_start;
  201. vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
  202. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  203. sizeof(vinfo), &vinfo))
  204. goto nla_put_failure;
  205. vinfo.vid = vid_end;
  206. vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
  207. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  208. sizeof(vinfo), &vinfo))
  209. goto nla_put_failure;
  210. } else {
  211. vinfo.vid = vid_start;
  212. vinfo.flags = flags;
  213. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  214. sizeof(vinfo), &vinfo))
  215. goto nla_put_failure;
  216. }
  217. return 0;
  218. nla_put_failure:
  219. return -EMSGSIZE;
  220. }
  221. static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
  222. struct net_bridge_vlan_group *vg)
  223. {
  224. struct net_bridge_vlan *v;
  225. u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
  226. u16 flags, pvid;
  227. int err = 0;
  228. /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
  229. * and mark vlan info with begin and end flags
  230. * if vlaninfo represents a range
  231. */
  232. pvid = br_get_pvid(vg);
  233. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  234. flags = 0;
  235. if (!br_vlan_should_use(v))
  236. continue;
  237. if (v->vid == pvid)
  238. flags |= BRIDGE_VLAN_INFO_PVID;
  239. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  240. flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  241. if (vid_range_start == 0) {
  242. goto initvars;
  243. } else if ((v->vid - vid_range_end) == 1 &&
  244. flags == vid_range_flags) {
  245. vid_range_end = v->vid;
  246. continue;
  247. } else {
  248. err = br_fill_ifvlaninfo_range(skb, vid_range_start,
  249. vid_range_end,
  250. vid_range_flags);
  251. if (err)
  252. return err;
  253. }
  254. initvars:
  255. vid_range_start = v->vid;
  256. vid_range_end = v->vid;
  257. vid_range_flags = flags;
  258. }
  259. if (vid_range_start != 0) {
  260. /* Call it once more to send any left over vlans */
  261. err = br_fill_ifvlaninfo_range(skb, vid_range_start,
  262. vid_range_end,
  263. vid_range_flags);
  264. if (err)
  265. return err;
  266. }
  267. return 0;
  268. }
  269. static int br_fill_ifvlaninfo(struct sk_buff *skb,
  270. struct net_bridge_vlan_group *vg)
  271. {
  272. struct bridge_vlan_info vinfo;
  273. struct net_bridge_vlan *v;
  274. u16 pvid;
  275. pvid = br_get_pvid(vg);
  276. list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
  277. if (!br_vlan_should_use(v))
  278. continue;
  279. vinfo.vid = v->vid;
  280. vinfo.flags = 0;
  281. if (v->vid == pvid)
  282. vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
  283. if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
  284. vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
  285. if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
  286. sizeof(vinfo), &vinfo))
  287. goto nla_put_failure;
  288. }
  289. return 0;
  290. nla_put_failure:
  291. return -EMSGSIZE;
  292. }
  293. /*
  294. * Create one netlink message for one interface
  295. * Contains port and master info as well as carrier and bridge state.
  296. */
  297. static int br_fill_ifinfo(struct sk_buff *skb,
  298. struct net_bridge_port *port,
  299. u32 pid, u32 seq, int event, unsigned int flags,
  300. u32 filter_mask, const struct net_device *dev)
  301. {
  302. struct net_bridge *br;
  303. struct ifinfomsg *hdr;
  304. struct nlmsghdr *nlh;
  305. u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
  306. if (port)
  307. br = port->br;
  308. else
  309. br = netdev_priv(dev);
  310. br_debug(br, "br_fill_info event %d port %s master %s\n",
  311. event, dev->name, br->dev->name);
  312. nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
  313. if (nlh == NULL)
  314. return -EMSGSIZE;
  315. hdr = nlmsg_data(nlh);
  316. hdr->ifi_family = AF_BRIDGE;
  317. hdr->__ifi_pad = 0;
  318. hdr->ifi_type = dev->type;
  319. hdr->ifi_index = dev->ifindex;
  320. hdr->ifi_flags = dev_get_flags(dev);
  321. hdr->ifi_change = 0;
  322. if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
  323. nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
  324. nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
  325. nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
  326. (dev->addr_len &&
  327. nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
  328. (dev->ifindex != dev_get_iflink(dev) &&
  329. nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
  330. goto nla_put_failure;
  331. if (event == RTM_NEWLINK && port) {
  332. struct nlattr *nest
  333. = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
  334. if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
  335. goto nla_put_failure;
  336. nla_nest_end(skb, nest);
  337. }
  338. /* Check if the VID information is requested */
  339. if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
  340. (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
  341. struct net_bridge_vlan_group *vg;
  342. struct nlattr *af;
  343. int err;
  344. /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
  345. rcu_read_lock();
  346. if (port)
  347. vg = nbp_vlan_group_rcu(port);
  348. else
  349. vg = br_vlan_group_rcu(br);
  350. if (!vg || !vg->num_vlans) {
  351. rcu_read_unlock();
  352. goto done;
  353. }
  354. af = nla_nest_start(skb, IFLA_AF_SPEC);
  355. if (!af) {
  356. rcu_read_unlock();
  357. goto nla_put_failure;
  358. }
  359. if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
  360. err = br_fill_ifvlaninfo_compressed(skb, vg);
  361. else
  362. err = br_fill_ifvlaninfo(skb, vg);
  363. rcu_read_unlock();
  364. if (err)
  365. goto nla_put_failure;
  366. nla_nest_end(skb, af);
  367. }
  368. done:
  369. nlmsg_end(skb, nlh);
  370. return 0;
  371. nla_put_failure:
  372. nlmsg_cancel(skb, nlh);
  373. return -EMSGSIZE;
  374. }
  375. /*
  376. * Notify listeners of a change in port information
  377. */
  378. void br_ifinfo_notify(int event, struct net_bridge_port *port)
  379. {
  380. struct net *net;
  381. struct sk_buff *skb;
  382. int err = -ENOBUFS;
  383. u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
  384. if (!port)
  385. return;
  386. net = dev_net(port->dev);
  387. br_debug(port->br, "port %u(%s) event %d\n",
  388. (unsigned int)port->port_no, port->dev->name, event);
  389. skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
  390. if (skb == NULL)
  391. goto errout;
  392. err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
  393. if (err < 0) {
  394. /* -EMSGSIZE implies BUG in br_nlmsg_size() */
  395. WARN_ON(err == -EMSGSIZE);
  396. kfree_skb(skb);
  397. goto errout;
  398. }
  399. rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
  400. return;
  401. errout:
  402. rtnl_set_sk_err(net, RTNLGRP_LINK, err);
  403. }
  404. /*
  405. * Dump information about all ports, in response to GETLINK
  406. */
  407. int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
  408. struct net_device *dev, u32 filter_mask, int nlflags)
  409. {
  410. struct net_bridge_port *port = br_port_get_rtnl(dev);
  411. if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
  412. !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
  413. return 0;
  414. return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
  415. filter_mask, dev);
  416. }
  417. static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
  418. int cmd, struct bridge_vlan_info *vinfo)
  419. {
  420. int err = 0;
  421. switch (cmd) {
  422. case RTM_SETLINK:
  423. if (p) {
  424. /* if the MASTER flag is set this will act on the global
  425. * per-VLAN entry as well
  426. */
  427. err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
  428. if (err)
  429. break;
  430. } else {
  431. vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
  432. err = br_vlan_add(br, vinfo->vid, vinfo->flags);
  433. }
  434. break;
  435. case RTM_DELLINK:
  436. if (p) {
  437. nbp_vlan_delete(p, vinfo->vid);
  438. if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
  439. br_vlan_delete(p->br, vinfo->vid);
  440. } else {
  441. br_vlan_delete(br, vinfo->vid);
  442. }
  443. break;
  444. }
  445. return err;
  446. }
  447. static int br_afspec(struct net_bridge *br,
  448. struct net_bridge_port *p,
  449. struct nlattr *af_spec,
  450. int cmd)
  451. {
  452. struct bridge_vlan_info *vinfo_start = NULL;
  453. struct bridge_vlan_info *vinfo = NULL;
  454. struct nlattr *attr;
  455. int err = 0;
  456. int rem;
  457. nla_for_each_nested(attr, af_spec, rem) {
  458. if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
  459. continue;
  460. if (nla_len(attr) != sizeof(struct bridge_vlan_info))
  461. return -EINVAL;
  462. vinfo = nla_data(attr);
  463. if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
  464. return -EINVAL;
  465. if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
  466. if (vinfo_start)
  467. return -EINVAL;
  468. vinfo_start = vinfo;
  469. /* don't allow range of pvids */
  470. if (vinfo_start->flags & BRIDGE_VLAN_INFO_PVID)
  471. return -EINVAL;
  472. continue;
  473. }
  474. if (vinfo_start) {
  475. struct bridge_vlan_info tmp_vinfo;
  476. int v;
  477. if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
  478. return -EINVAL;
  479. if (vinfo->vid <= vinfo_start->vid)
  480. return -EINVAL;
  481. memcpy(&tmp_vinfo, vinfo_start,
  482. sizeof(struct bridge_vlan_info));
  483. for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
  484. tmp_vinfo.vid = v;
  485. err = br_vlan_info(br, p, cmd, &tmp_vinfo);
  486. if (err)
  487. break;
  488. }
  489. vinfo_start = NULL;
  490. } else {
  491. err = br_vlan_info(br, p, cmd, vinfo);
  492. }
  493. if (err)
  494. break;
  495. }
  496. return err;
  497. }
  498. static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
  499. [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
  500. [IFLA_BRPORT_COST] = { .type = NLA_U32 },
  501. [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
  502. [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
  503. [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
  504. [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
  505. [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
  506. [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
  507. [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
  508. [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
  509. [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
  510. [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
  511. };
  512. /* Change the state of the port and notify spanning tree */
  513. static int br_set_port_state(struct net_bridge_port *p, u8 state)
  514. {
  515. if (state > BR_STATE_BLOCKING)
  516. return -EINVAL;
  517. /* if kernel STP is running, don't allow changes */
  518. if (p->br->stp_enabled == BR_KERNEL_STP)
  519. return -EBUSY;
  520. /* if device is not up, change is not allowed
  521. * if link is not present, only allowable state is disabled
  522. */
  523. if (!netif_running(p->dev) ||
  524. (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
  525. return -ENETDOWN;
  526. br_set_state(p, state);
  527. br_port_state_selection(p->br);
  528. return 0;
  529. }
  530. /* Set/clear or port flags based on attribute */
  531. static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
  532. int attrtype, unsigned long mask)
  533. {
  534. if (tb[attrtype]) {
  535. u8 flag = nla_get_u8(tb[attrtype]);
  536. if (flag)
  537. p->flags |= mask;
  538. else
  539. p->flags &= ~mask;
  540. }
  541. }
  542. /* Process bridge protocol info on port */
  543. static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
  544. {
  545. int err;
  546. unsigned long old_flags = p->flags;
  547. br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
  548. br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
  549. br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
  550. br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
  551. br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
  552. br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
  553. br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
  554. br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
  555. br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
  556. if (tb[IFLA_BRPORT_COST]) {
  557. err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
  558. if (err)
  559. return err;
  560. }
  561. if (tb[IFLA_BRPORT_PRIORITY]) {
  562. err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
  563. if (err)
  564. return err;
  565. }
  566. if (tb[IFLA_BRPORT_STATE]) {
  567. err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
  568. if (err)
  569. return err;
  570. }
  571. if (tb[IFLA_BRPORT_FLUSH])
  572. br_fdb_delete_by_port(p->br, p, 0, 0);
  573. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  574. if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
  575. u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
  576. err = br_multicast_set_port_router(p, mcast_router);
  577. if (err)
  578. return err;
  579. }
  580. #endif
  581. br_port_flags_change(p, old_flags ^ p->flags);
  582. return 0;
  583. }
  584. /* Change state and parameters on port. */
  585. int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
  586. {
  587. struct nlattr *protinfo;
  588. struct nlattr *afspec;
  589. struct net_bridge_port *p;
  590. struct nlattr *tb[IFLA_BRPORT_MAX + 1];
  591. int err = 0;
  592. protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
  593. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  594. if (!protinfo && !afspec)
  595. return 0;
  596. p = br_port_get_rtnl(dev);
  597. /* We want to accept dev as bridge itself if the AF_SPEC
  598. * is set to see if someone is setting vlan info on the bridge
  599. */
  600. if (!p && !afspec)
  601. return -EINVAL;
  602. if (p && protinfo) {
  603. if (protinfo->nla_type & NLA_F_NESTED) {
  604. err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
  605. protinfo, br_port_policy);
  606. if (err)
  607. return err;
  608. spin_lock_bh(&p->br->lock);
  609. err = br_setport(p, tb);
  610. spin_unlock_bh(&p->br->lock);
  611. } else {
  612. /* Binary compatibility with old RSTP */
  613. if (nla_len(protinfo) < sizeof(u8))
  614. return -EINVAL;
  615. spin_lock_bh(&p->br->lock);
  616. err = br_set_port_state(p, nla_get_u8(protinfo));
  617. spin_unlock_bh(&p->br->lock);
  618. }
  619. if (err)
  620. goto out;
  621. }
  622. if (afspec) {
  623. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  624. afspec, RTM_SETLINK);
  625. }
  626. if (err == 0)
  627. br_ifinfo_notify(RTM_NEWLINK, p);
  628. out:
  629. return err;
  630. }
  631. /* Delete port information */
  632. int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
  633. {
  634. struct nlattr *afspec;
  635. struct net_bridge_port *p;
  636. int err = 0;
  637. afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
  638. if (!afspec)
  639. return 0;
  640. p = br_port_get_rtnl(dev);
  641. /* We want to accept dev as bridge itself as well */
  642. if (!p && !(dev->priv_flags & IFF_EBRIDGE))
  643. return -EINVAL;
  644. err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
  645. afspec, RTM_DELLINK);
  646. if (err == 0)
  647. /* Send RTM_NEWLINK because userspace
  648. * expects RTM_NEWLINK for vlan dels
  649. */
  650. br_ifinfo_notify(RTM_NEWLINK, p);
  651. return err;
  652. }
  653. static int br_validate(struct nlattr *tb[], struct nlattr *data[])
  654. {
  655. if (tb[IFLA_ADDRESS]) {
  656. if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
  657. return -EINVAL;
  658. if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
  659. return -EADDRNOTAVAIL;
  660. }
  661. if (!data)
  662. return 0;
  663. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  664. if (data[IFLA_BR_VLAN_PROTOCOL]) {
  665. switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
  666. case htons(ETH_P_8021Q):
  667. case htons(ETH_P_8021AD):
  668. break;
  669. default:
  670. return -EPROTONOSUPPORT;
  671. }
  672. }
  673. if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
  674. __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
  675. if (defpvid >= VLAN_VID_MASK)
  676. return -EINVAL;
  677. }
  678. #endif
  679. return 0;
  680. }
  681. static int br_port_slave_changelink(struct net_device *brdev,
  682. struct net_device *dev,
  683. struct nlattr *tb[],
  684. struct nlattr *data[])
  685. {
  686. struct net_bridge *br = netdev_priv(brdev);
  687. int ret;
  688. if (!data)
  689. return 0;
  690. spin_lock_bh(&br->lock);
  691. ret = br_setport(br_port_get_rtnl(dev), data);
  692. spin_unlock_bh(&br->lock);
  693. return ret;
  694. }
  695. static int br_port_fill_slave_info(struct sk_buff *skb,
  696. const struct net_device *brdev,
  697. const struct net_device *dev)
  698. {
  699. return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
  700. }
  701. static size_t br_port_get_slave_size(const struct net_device *brdev,
  702. const struct net_device *dev)
  703. {
  704. return br_port_info_size();
  705. }
  706. static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
  707. [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
  708. [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
  709. [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
  710. [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
  711. [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
  712. [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
  713. [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
  714. [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
  715. [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
  716. [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
  717. .len = ETH_ALEN },
  718. [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
  719. [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
  720. [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
  721. [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
  722. [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
  723. [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
  724. [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
  725. [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
  726. [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
  727. [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
  728. [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
  729. [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
  730. [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
  731. [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
  732. [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
  733. [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
  734. [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
  735. [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
  736. [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
  737. [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
  738. };
  739. static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
  740. struct nlattr *data[])
  741. {
  742. struct net_bridge *br = netdev_priv(brdev);
  743. int err;
  744. if (!data)
  745. return 0;
  746. if (data[IFLA_BR_FORWARD_DELAY]) {
  747. err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
  748. if (err)
  749. return err;
  750. }
  751. if (data[IFLA_BR_HELLO_TIME]) {
  752. err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
  753. if (err)
  754. return err;
  755. }
  756. if (data[IFLA_BR_MAX_AGE]) {
  757. err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
  758. if (err)
  759. return err;
  760. }
  761. if (data[IFLA_BR_AGEING_TIME]) {
  762. err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
  763. if (err)
  764. return err;
  765. }
  766. if (data[IFLA_BR_STP_STATE]) {
  767. u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
  768. br_stp_set_enabled(br, stp_enabled);
  769. }
  770. if (data[IFLA_BR_PRIORITY]) {
  771. u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
  772. br_stp_set_bridge_priority(br, priority);
  773. }
  774. if (data[IFLA_BR_VLAN_FILTERING]) {
  775. u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
  776. err = __br_vlan_filter_toggle(br, vlan_filter);
  777. if (err)
  778. return err;
  779. }
  780. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  781. if (data[IFLA_BR_VLAN_PROTOCOL]) {
  782. __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
  783. err = __br_vlan_set_proto(br, vlan_proto);
  784. if (err)
  785. return err;
  786. }
  787. if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
  788. __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
  789. err = __br_vlan_set_default_pvid(br, defpvid);
  790. if (err)
  791. return err;
  792. }
  793. if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
  794. __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
  795. err = br_vlan_set_stats(br, vlan_stats);
  796. if (err)
  797. return err;
  798. }
  799. #endif
  800. if (data[IFLA_BR_GROUP_FWD_MASK]) {
  801. u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
  802. if (fwd_mask & BR_GROUPFWD_RESTRICTED)
  803. return -EINVAL;
  804. br->group_fwd_mask = fwd_mask;
  805. }
  806. if (data[IFLA_BR_GROUP_ADDR]) {
  807. u8 new_addr[ETH_ALEN];
  808. if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
  809. return -EINVAL;
  810. memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
  811. if (!is_link_local_ether_addr(new_addr))
  812. return -EINVAL;
  813. if (new_addr[5] == 1 || /* 802.3x Pause address */
  814. new_addr[5] == 2 || /* 802.3ad Slow protocols */
  815. new_addr[5] == 3) /* 802.1X PAE address */
  816. return -EINVAL;
  817. spin_lock_bh(&br->lock);
  818. memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
  819. spin_unlock_bh(&br->lock);
  820. br->group_addr_set = true;
  821. br_recalculate_fwd_mask(br);
  822. }
  823. if (data[IFLA_BR_FDB_FLUSH])
  824. br_fdb_flush(br);
  825. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  826. if (data[IFLA_BR_MCAST_ROUTER]) {
  827. u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
  828. err = br_multicast_set_router(br, multicast_router);
  829. if (err)
  830. return err;
  831. }
  832. if (data[IFLA_BR_MCAST_SNOOPING]) {
  833. u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
  834. err = br_multicast_toggle(br, mcast_snooping);
  835. if (err)
  836. return err;
  837. }
  838. if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
  839. u8 val;
  840. val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
  841. br->multicast_query_use_ifaddr = !!val;
  842. }
  843. if (data[IFLA_BR_MCAST_QUERIER]) {
  844. u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
  845. err = br_multicast_set_querier(br, mcast_querier);
  846. if (err)
  847. return err;
  848. }
  849. if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
  850. u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
  851. br->hash_elasticity = val;
  852. }
  853. if (data[IFLA_BR_MCAST_HASH_MAX]) {
  854. u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
  855. err = br_multicast_set_hash_max(br, hash_max);
  856. if (err)
  857. return err;
  858. }
  859. if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
  860. u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
  861. br->multicast_last_member_count = val;
  862. }
  863. if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
  864. u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
  865. br->multicast_startup_query_count = val;
  866. }
  867. if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
  868. u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
  869. br->multicast_last_member_interval = clock_t_to_jiffies(val);
  870. }
  871. if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
  872. u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
  873. br->multicast_membership_interval = clock_t_to_jiffies(val);
  874. }
  875. if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
  876. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
  877. br->multicast_querier_interval = clock_t_to_jiffies(val);
  878. }
  879. if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
  880. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
  881. br->multicast_query_interval = clock_t_to_jiffies(val);
  882. }
  883. if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
  884. u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
  885. br->multicast_query_response_interval = clock_t_to_jiffies(val);
  886. }
  887. if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
  888. u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
  889. br->multicast_startup_query_interval = clock_t_to_jiffies(val);
  890. }
  891. if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
  892. __u8 mcast_stats;
  893. mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
  894. br->multicast_stats_enabled = !!mcast_stats;
  895. }
  896. #endif
  897. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  898. if (data[IFLA_BR_NF_CALL_IPTABLES]) {
  899. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
  900. br->nf_call_iptables = val ? true : false;
  901. }
  902. if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
  903. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
  904. br->nf_call_ip6tables = val ? true : false;
  905. }
  906. if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
  907. u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
  908. br->nf_call_arptables = val ? true : false;
  909. }
  910. #endif
  911. return 0;
  912. }
  913. static int br_dev_newlink(struct net *src_net, struct net_device *dev,
  914. struct nlattr *tb[], struct nlattr *data[])
  915. {
  916. struct net_bridge *br = netdev_priv(dev);
  917. int err;
  918. err = register_netdevice(dev);
  919. if (err)
  920. return err;
  921. if (tb[IFLA_ADDRESS]) {
  922. spin_lock_bh(&br->lock);
  923. br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
  924. spin_unlock_bh(&br->lock);
  925. }
  926. err = br_changelink(dev, tb, data);
  927. if (err)
  928. br_dev_delete(dev, NULL);
  929. return err;
  930. }
  931. static size_t br_get_size(const struct net_device *brdev)
  932. {
  933. return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
  934. nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
  935. nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
  936. nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
  937. nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
  938. nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
  939. nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
  940. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  941. nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
  942. nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
  943. nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
  944. #endif
  945. nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
  946. nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
  947. nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
  948. nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
  949. nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
  950. nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
  951. nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
  952. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
  953. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
  954. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
  955. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
  956. nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
  957. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  958. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
  959. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
  960. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
  961. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
  962. nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
  963. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
  964. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
  965. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
  966. nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
  967. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
  968. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
  969. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
  970. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
  971. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
  972. nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
  973. #endif
  974. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  975. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
  976. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
  977. nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
  978. #endif
  979. 0;
  980. }
  981. static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
  982. {
  983. struct net_bridge *br = netdev_priv(brdev);
  984. u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
  985. u32 hello_time = jiffies_to_clock_t(br->hello_time);
  986. u32 age_time = jiffies_to_clock_t(br->max_age);
  987. u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
  988. u32 stp_enabled = br->stp_enabled;
  989. u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
  990. u8 vlan_enabled = br_vlan_enabled(br);
  991. u64 clockval;
  992. clockval = br_timer_value(&br->hello_timer);
  993. if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
  994. return -EMSGSIZE;
  995. clockval = br_timer_value(&br->tcn_timer);
  996. if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
  997. return -EMSGSIZE;
  998. clockval = br_timer_value(&br->topology_change_timer);
  999. if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
  1000. IFLA_BR_PAD))
  1001. return -EMSGSIZE;
  1002. clockval = br_timer_value(&br->gc_timer);
  1003. if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
  1004. return -EMSGSIZE;
  1005. if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
  1006. nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
  1007. nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
  1008. nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
  1009. nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
  1010. nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
  1011. nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
  1012. nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
  1013. nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
  1014. &br->bridge_id) ||
  1015. nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
  1016. &br->designated_root) ||
  1017. nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
  1018. nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
  1019. nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
  1020. nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
  1021. br->topology_change_detected) ||
  1022. nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
  1023. return -EMSGSIZE;
  1024. #ifdef CONFIG_BRIDGE_VLAN_FILTERING
  1025. if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
  1026. nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
  1027. nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED, br->vlan_stats_enabled))
  1028. return -EMSGSIZE;
  1029. #endif
  1030. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  1031. if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
  1032. nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
  1033. nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
  1034. br->multicast_query_use_ifaddr) ||
  1035. nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
  1036. nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
  1037. br->multicast_stats_enabled) ||
  1038. nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
  1039. br->hash_elasticity) ||
  1040. nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
  1041. nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
  1042. br->multicast_last_member_count) ||
  1043. nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
  1044. br->multicast_startup_query_count))
  1045. return -EMSGSIZE;
  1046. clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
  1047. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
  1048. IFLA_BR_PAD))
  1049. return -EMSGSIZE;
  1050. clockval = jiffies_to_clock_t(br->multicast_membership_interval);
  1051. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
  1052. IFLA_BR_PAD))
  1053. return -EMSGSIZE;
  1054. clockval = jiffies_to_clock_t(br->multicast_querier_interval);
  1055. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
  1056. IFLA_BR_PAD))
  1057. return -EMSGSIZE;
  1058. clockval = jiffies_to_clock_t(br->multicast_query_interval);
  1059. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
  1060. IFLA_BR_PAD))
  1061. return -EMSGSIZE;
  1062. clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
  1063. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
  1064. IFLA_BR_PAD))
  1065. return -EMSGSIZE;
  1066. clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
  1067. if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
  1068. IFLA_BR_PAD))
  1069. return -EMSGSIZE;
  1070. #endif
  1071. #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  1072. if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
  1073. br->nf_call_iptables ? 1 : 0) ||
  1074. nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
  1075. br->nf_call_ip6tables ? 1 : 0) ||
  1076. nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
  1077. br->nf_call_arptables ? 1 : 0))
  1078. return -EMSGSIZE;
  1079. #endif
  1080. return 0;
  1081. }
  1082. static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
  1083. {
  1084. struct net_bridge_port *p = NULL;
  1085. struct net_bridge_vlan_group *vg;
  1086. struct net_bridge_vlan *v;
  1087. struct net_bridge *br;
  1088. int numvls = 0;
  1089. switch (attr) {
  1090. case IFLA_STATS_LINK_XSTATS:
  1091. br = netdev_priv(dev);
  1092. vg = br_vlan_group(br);
  1093. break;
  1094. case IFLA_STATS_LINK_XSTATS_SLAVE:
  1095. p = br_port_get_rtnl(dev);
  1096. if (!p)
  1097. return 0;
  1098. br = p->br;
  1099. vg = nbp_vlan_group(p);
  1100. break;
  1101. default:
  1102. return 0;
  1103. }
  1104. if (vg) {
  1105. /* we need to count all, even placeholder entries */
  1106. list_for_each_entry(v, &vg->vlan_list, vlist)
  1107. numvls++;
  1108. }
  1109. return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
  1110. nla_total_size(sizeof(struct br_mcast_stats)) +
  1111. nla_total_size(0);
  1112. }
  1113. static int br_fill_linkxstats(struct sk_buff *skb,
  1114. const struct net_device *dev,
  1115. int *prividx, int attr)
  1116. {
  1117. struct nlattr *nla __maybe_unused;
  1118. struct net_bridge_port *p = NULL;
  1119. struct net_bridge_vlan_group *vg;
  1120. struct net_bridge_vlan *v;
  1121. struct net_bridge *br;
  1122. struct nlattr *nest;
  1123. int vl_idx = 0;
  1124. switch (attr) {
  1125. case IFLA_STATS_LINK_XSTATS:
  1126. br = netdev_priv(dev);
  1127. vg = br_vlan_group(br);
  1128. break;
  1129. case IFLA_STATS_LINK_XSTATS_SLAVE:
  1130. p = br_port_get_rtnl(dev);
  1131. if (!p)
  1132. return 0;
  1133. br = p->br;
  1134. vg = nbp_vlan_group(p);
  1135. break;
  1136. default:
  1137. return -EINVAL;
  1138. }
  1139. nest = nla_nest_start(skb, LINK_XSTATS_TYPE_BRIDGE);
  1140. if (!nest)
  1141. return -EMSGSIZE;
  1142. if (vg) {
  1143. u16 pvid;
  1144. pvid = br_get_pvid(vg);
  1145. list_for_each_entry(v, &vg->vlan_list, vlist) {
  1146. struct bridge_vlan_xstats vxi;
  1147. struct br_vlan_stats stats;
  1148. if (++vl_idx < *prividx)
  1149. continue;
  1150. memset(&vxi, 0, sizeof(vxi));
  1151. vxi.vid = v->vid;
  1152. vxi.flags = v->flags;
  1153. if (v->vid == pvid)
  1154. vxi.flags |= BRIDGE_VLAN_INFO_PVID;
  1155. br_vlan_get_stats(v, &stats);
  1156. vxi.rx_bytes = stats.rx_bytes;
  1157. vxi.rx_packets = stats.rx_packets;
  1158. vxi.tx_bytes = stats.tx_bytes;
  1159. vxi.tx_packets = stats.tx_packets;
  1160. if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
  1161. goto nla_put_failure;
  1162. }
  1163. }
  1164. #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
  1165. if (++vl_idx >= *prividx) {
  1166. nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
  1167. sizeof(struct br_mcast_stats),
  1168. BRIDGE_XSTATS_PAD);
  1169. if (!nla)
  1170. goto nla_put_failure;
  1171. br_multicast_get_stats(br, p, nla_data(nla));
  1172. }
  1173. #endif
  1174. nla_nest_end(skb, nest);
  1175. *prividx = 0;
  1176. return 0;
  1177. nla_put_failure:
  1178. nla_nest_end(skb, nest);
  1179. *prividx = vl_idx;
  1180. return -EMSGSIZE;
  1181. }
  1182. static struct rtnl_af_ops br_af_ops __read_mostly = {
  1183. .family = AF_BRIDGE,
  1184. .get_link_af_size = br_get_link_af_size_filtered,
  1185. };
  1186. struct rtnl_link_ops br_link_ops __read_mostly = {
  1187. .kind = "bridge",
  1188. .priv_size = sizeof(struct net_bridge),
  1189. .setup = br_dev_setup,
  1190. .maxtype = IFLA_BR_MAX,
  1191. .policy = br_policy,
  1192. .validate = br_validate,
  1193. .newlink = br_dev_newlink,
  1194. .changelink = br_changelink,
  1195. .dellink = br_dev_delete,
  1196. .get_size = br_get_size,
  1197. .fill_info = br_fill_info,
  1198. .fill_linkxstats = br_fill_linkxstats,
  1199. .get_linkxstats_size = br_get_linkxstats_size,
  1200. .slave_maxtype = IFLA_BRPORT_MAX,
  1201. .slave_policy = br_port_policy,
  1202. .slave_changelink = br_port_slave_changelink,
  1203. .get_slave_size = br_port_get_slave_size,
  1204. .fill_slave_info = br_port_fill_slave_info,
  1205. };
  1206. int __init br_netlink_init(void)
  1207. {
  1208. int err;
  1209. br_mdb_init();
  1210. rtnl_af_register(&br_af_ops);
  1211. err = rtnl_link_register(&br_link_ops);
  1212. if (err)
  1213. goto out_af;
  1214. return 0;
  1215. out_af:
  1216. rtnl_af_unregister(&br_af_ops);
  1217. br_mdb_uninit();
  1218. return err;
  1219. }
  1220. void br_netlink_fini(void)
  1221. {
  1222. br_mdb_uninit();
  1223. rtnl_af_unregister(&br_af_ops);
  1224. rtnl_link_unregister(&br_link_ops);
  1225. }