hsr_device.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright 2011-2014 Autronica Fire and Security AS
  3. *
  4. * Author(s):
  5. * 2011-2014 Arvid Brodin, arvid.brodin@alten.se
  6. * This file contains device methods for creating, using and destroying
  7. * virtual HSR or PRP devices.
  8. */
  9. #include <linux/netdevice.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/etherdevice.h>
  12. #include <linux/rtnetlink.h>
  13. #include <linux/pkt_sched.h>
  14. #include "hsr_device.h"
  15. #include "hsr_slave.h"
  16. #include "hsr_framereg.h"
  17. #include "hsr_main.h"
  18. #include "hsr_forward.h"
  19. static bool is_admin_up(struct net_device *dev)
  20. {
  21. return dev && (dev->flags & IFF_UP);
  22. }
  23. static bool is_slave_up(struct net_device *dev)
  24. {
  25. return dev && is_admin_up(dev) && netif_oper_up(dev);
  26. }
  27. static void hsr_set_operstate(struct hsr_port *master, bool has_carrier)
  28. {
  29. struct net_device *dev = master->dev;
  30. if (!is_admin_up(dev)) {
  31. netif_set_operstate(dev, IF_OPER_DOWN);
  32. return;
  33. }
  34. if (has_carrier)
  35. netif_set_operstate(dev, IF_OPER_UP);
  36. else
  37. netif_set_operstate(dev, IF_OPER_LOWERLAYERDOWN);
  38. }
  39. static bool hsr_check_carrier(struct hsr_port *master)
  40. {
  41. struct hsr_port *port;
  42. ASSERT_RTNL();
  43. hsr_for_each_port_rtnl(master->hsr, port) {
  44. if (port->type != HSR_PT_MASTER && is_slave_up(port->dev)) {
  45. netif_carrier_on(master->dev);
  46. return true;
  47. }
  48. }
  49. netif_carrier_off(master->dev);
  50. return false;
  51. }
  52. static void hsr_check_announce(struct net_device *hsr_dev)
  53. {
  54. struct hsr_priv *hsr;
  55. hsr = netdev_priv(hsr_dev);
  56. if (netif_running(hsr_dev) && netif_oper_up(hsr_dev)) {
  57. /* Enable announce timer and start sending supervisory frames */
  58. if (!timer_pending(&hsr->announce_timer)) {
  59. hsr->announce_count = 0;
  60. mod_timer(&hsr->announce_timer, jiffies +
  61. msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL));
  62. }
  63. if (hsr->redbox && !timer_pending(&hsr->announce_proxy_timer))
  64. mod_timer(&hsr->announce_proxy_timer, jiffies +
  65. msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL) / 2);
  66. } else {
  67. /* Deactivate the announce timer */
  68. timer_delete(&hsr->announce_timer);
  69. if (hsr->redbox)
  70. timer_delete(&hsr->announce_proxy_timer);
  71. }
  72. }
  73. void hsr_check_carrier_and_operstate(struct hsr_priv *hsr)
  74. {
  75. struct hsr_port *master;
  76. bool has_carrier;
  77. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  78. /* netif_stacked_transfer_operstate() cannot be used here since
  79. * it doesn't set IF_OPER_LOWERLAYERDOWN (?)
  80. */
  81. has_carrier = hsr_check_carrier(master);
  82. hsr_set_operstate(master, has_carrier);
  83. hsr_check_announce(master->dev);
  84. }
  85. int hsr_get_max_mtu(struct hsr_priv *hsr)
  86. {
  87. unsigned int mtu_max;
  88. struct hsr_port *port;
  89. mtu_max = ETH_DATA_LEN;
  90. hsr_for_each_port_rtnl(hsr, port)
  91. if (port->type != HSR_PT_MASTER)
  92. mtu_max = min(port->dev->mtu, mtu_max);
  93. if (mtu_max < HSR_HLEN)
  94. return 0;
  95. return mtu_max - HSR_HLEN;
  96. }
  97. static int hsr_dev_change_mtu(struct net_device *dev, int new_mtu)
  98. {
  99. struct hsr_priv *hsr;
  100. hsr = netdev_priv(dev);
  101. if (new_mtu > hsr_get_max_mtu(hsr)) {
  102. netdev_info(dev, "A HSR master's MTU cannot be greater than the smallest MTU of its slaves minus the HSR Tag length (%d octets).\n",
  103. HSR_HLEN);
  104. return -EINVAL;
  105. }
  106. WRITE_ONCE(dev->mtu, new_mtu);
  107. return 0;
  108. }
  109. static int hsr_dev_open(struct net_device *dev)
  110. {
  111. struct hsr_priv *hsr;
  112. struct hsr_port *port;
  113. const char *designation = NULL;
  114. hsr = netdev_priv(dev);
  115. hsr_for_each_port_rtnl(hsr, port) {
  116. if (port->type == HSR_PT_MASTER)
  117. continue;
  118. switch (port->type) {
  119. case HSR_PT_SLAVE_A:
  120. designation = "Slave A";
  121. break;
  122. case HSR_PT_SLAVE_B:
  123. designation = "Slave B";
  124. break;
  125. case HSR_PT_INTERLINK:
  126. designation = "Interlink";
  127. break;
  128. default:
  129. designation = "Unknown";
  130. }
  131. if (!is_slave_up(port->dev))
  132. netdev_warn(dev, "%s (%s) is not up; please bring it up to get a fully working HSR network\n",
  133. designation, port->dev->name);
  134. }
  135. if (!designation)
  136. netdev_warn(dev, "No slave devices configured\n");
  137. return 0;
  138. }
  139. static int hsr_dev_close(struct net_device *dev)
  140. {
  141. struct hsr_port *port;
  142. struct hsr_priv *hsr;
  143. hsr = netdev_priv(dev);
  144. hsr_for_each_port_rtnl(hsr, port) {
  145. if (port->type == HSR_PT_MASTER)
  146. continue;
  147. switch (port->type) {
  148. case HSR_PT_SLAVE_A:
  149. case HSR_PT_SLAVE_B:
  150. dev_uc_unsync(port->dev, dev);
  151. dev_mc_unsync(port->dev, dev);
  152. break;
  153. default:
  154. break;
  155. }
  156. }
  157. return 0;
  158. }
  159. static netdev_features_t hsr_features_recompute(struct hsr_priv *hsr,
  160. netdev_features_t features)
  161. {
  162. netdev_features_t mask;
  163. struct hsr_port *port;
  164. mask = features;
  165. /* Mask out all features that, if supported by one device, should be
  166. * enabled for all devices (see NETIF_F_ONE_FOR_ALL).
  167. *
  168. * Anything that's off in mask will not be enabled - so only things
  169. * that were in features originally, and also is in NETIF_F_ONE_FOR_ALL,
  170. * may become enabled.
  171. */
  172. features &= ~NETIF_F_ONE_FOR_ALL;
  173. hsr_for_each_port_rtnl(hsr, port)
  174. features = netdev_increment_features(features,
  175. port->dev->features,
  176. mask);
  177. return features;
  178. }
  179. static netdev_features_t hsr_fix_features(struct net_device *dev,
  180. netdev_features_t features)
  181. {
  182. struct hsr_priv *hsr = netdev_priv(dev);
  183. return hsr_features_recompute(hsr, features);
  184. }
  185. static netdev_tx_t hsr_dev_xmit(struct sk_buff *skb, struct net_device *dev)
  186. {
  187. struct hsr_priv *hsr = netdev_priv(dev);
  188. struct hsr_port *master;
  189. rcu_read_lock();
  190. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  191. if (master) {
  192. skb->dev = master->dev;
  193. skb_reset_mac_header(skb);
  194. skb_reset_mac_len(skb);
  195. spin_lock_bh(&hsr->seqnr_lock);
  196. hsr_forward_skb(skb, master);
  197. spin_unlock_bh(&hsr->seqnr_lock);
  198. } else {
  199. dev_core_stats_tx_dropped_inc(dev);
  200. dev_kfree_skb_any(skb);
  201. }
  202. rcu_read_unlock();
  203. return NETDEV_TX_OK;
  204. }
  205. static const struct header_ops hsr_header_ops = {
  206. .create = eth_header,
  207. .parse = eth_header_parse,
  208. };
  209. static struct sk_buff *hsr_init_skb(struct hsr_port *master, int extra)
  210. {
  211. struct hsr_priv *hsr = master->hsr;
  212. struct sk_buff *skb;
  213. int hlen, tlen;
  214. int len;
  215. hlen = LL_RESERVED_SPACE(master->dev);
  216. tlen = master->dev->needed_tailroom;
  217. len = sizeof(struct hsr_sup_tag) + sizeof(struct hsr_sup_payload);
  218. /* skb size is same for PRP/HSR frames, only difference
  219. * being, for PRP it is a trailer and for HSR it is a
  220. * header.
  221. * RedBox might use @extra more bytes.
  222. */
  223. skb = dev_alloc_skb(len + extra + hlen + tlen);
  224. if (!skb)
  225. return skb;
  226. skb_reserve(skb, hlen);
  227. skb->dev = master->dev;
  228. skb->priority = TC_PRIO_CONTROL;
  229. skb_reset_network_header(skb);
  230. skb_reset_transport_header(skb);
  231. if (dev_hard_header(skb, skb->dev, ETH_P_PRP,
  232. hsr->sup_multicast_addr,
  233. skb->dev->dev_addr, skb->len) <= 0)
  234. goto out;
  235. skb_reset_mac_header(skb);
  236. skb_reset_mac_len(skb);
  237. return skb;
  238. out:
  239. kfree_skb(skb);
  240. return NULL;
  241. }
  242. static void send_hsr_supervision_frame(struct hsr_port *port,
  243. unsigned long *interval,
  244. const unsigned char *addr)
  245. {
  246. struct hsr_priv *hsr = port->hsr;
  247. __u8 type = HSR_TLV_LIFE_CHECK;
  248. struct hsr_sup_payload *hsr_sp;
  249. struct hsr_sup_tlv *hsr_stlv;
  250. struct hsr_sup_tag *hsr_stag;
  251. struct sk_buff *skb;
  252. int extra = 0;
  253. *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
  254. if (hsr->announce_count < 3 && hsr->prot_version == 0) {
  255. type = HSR_TLV_ANNOUNCE;
  256. *interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
  257. hsr->announce_count++;
  258. }
  259. if (hsr->redbox)
  260. extra = sizeof(struct hsr_sup_tlv) +
  261. sizeof(struct hsr_sup_payload);
  262. skb = hsr_init_skb(port, extra);
  263. if (!skb) {
  264. netdev_warn_once(port->dev, "HSR: Could not send supervision frame\n");
  265. return;
  266. }
  267. hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
  268. skb_set_network_header(skb, ETH_HLEN + HSR_HLEN);
  269. skb_reset_mac_len(skb);
  270. set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
  271. set_hsr_stag_HSR_ver(hsr_stag, hsr->prot_version);
  272. /* From HSRv1 on we have separate supervision sequence numbers. */
  273. spin_lock_bh(&hsr->seqnr_lock);
  274. if (hsr->prot_version > 0) {
  275. hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
  276. hsr->sup_sequence_nr++;
  277. } else {
  278. hsr_stag->sequence_nr = htons(hsr->sequence_nr);
  279. hsr->sequence_nr++;
  280. }
  281. hsr_stag->tlv.HSR_TLV_type = type;
  282. /* HSRv0 has 6 unused bytes after the MAC */
  283. hsr_stag->tlv.HSR_TLV_length = hsr->prot_version ?
  284. sizeof(struct hsr_sup_payload) : 12;
  285. /* Payload: MacAddressA / SAN MAC from ProxyNodeTable */
  286. hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
  287. ether_addr_copy(hsr_sp->macaddress_A, addr);
  288. if (hsr->redbox &&
  289. hsr_is_node_in_db(&hsr->proxy_node_db, addr)) {
  290. hsr_stlv = skb_put(skb, sizeof(struct hsr_sup_tlv));
  291. hsr_stlv->HSR_TLV_type = PRP_TLV_REDBOX_MAC;
  292. hsr_stlv->HSR_TLV_length = sizeof(struct hsr_sup_payload);
  293. /* Payload: MacAddressRedBox */
  294. hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
  295. ether_addr_copy(hsr_sp->macaddress_A, hsr->macaddress_redbox);
  296. }
  297. if (skb_put_padto(skb, ETH_ZLEN)) {
  298. spin_unlock_bh(&hsr->seqnr_lock);
  299. return;
  300. }
  301. hsr_forward_skb(skb, port);
  302. spin_unlock_bh(&hsr->seqnr_lock);
  303. return;
  304. }
  305. static void send_prp_supervision_frame(struct hsr_port *master,
  306. unsigned long *interval,
  307. const unsigned char *addr)
  308. {
  309. struct hsr_priv *hsr = master->hsr;
  310. struct hsr_sup_payload *hsr_sp;
  311. struct hsr_sup_tag *hsr_stag;
  312. struct sk_buff *skb;
  313. skb = hsr_init_skb(master, 0);
  314. if (!skb) {
  315. netdev_warn_once(master->dev, "PRP: Could not send supervision frame\n");
  316. return;
  317. }
  318. *interval = msecs_to_jiffies(HSR_LIFE_CHECK_INTERVAL);
  319. hsr_stag = skb_put(skb, sizeof(struct hsr_sup_tag));
  320. set_hsr_stag_path(hsr_stag, (hsr->prot_version ? 0x0 : 0xf));
  321. set_hsr_stag_HSR_ver(hsr_stag, (hsr->prot_version ? 1 : 0));
  322. /* From HSRv1 on we have separate supervision sequence numbers. */
  323. spin_lock_bh(&hsr->seqnr_lock);
  324. hsr_stag->sequence_nr = htons(hsr->sup_sequence_nr);
  325. hsr->sup_sequence_nr++;
  326. hsr_stag->tlv.HSR_TLV_type = PRP_TLV_LIFE_CHECK_DD;
  327. hsr_stag->tlv.HSR_TLV_length = sizeof(struct hsr_sup_payload);
  328. /* Payload: MacAddressA */
  329. hsr_sp = skb_put(skb, sizeof(struct hsr_sup_payload));
  330. ether_addr_copy(hsr_sp->macaddress_A, master->dev->dev_addr);
  331. if (skb_put_padto(skb, ETH_ZLEN)) {
  332. spin_unlock_bh(&hsr->seqnr_lock);
  333. return;
  334. }
  335. hsr_forward_skb(skb, master);
  336. spin_unlock_bh(&hsr->seqnr_lock);
  337. }
  338. /* Announce (supervision frame) timer function
  339. */
  340. static void hsr_announce(struct timer_list *t)
  341. {
  342. struct hsr_priv *hsr;
  343. struct hsr_port *master;
  344. unsigned long interval;
  345. hsr = timer_container_of(hsr, t, announce_timer);
  346. rcu_read_lock();
  347. master = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  348. hsr->proto_ops->send_sv_frame(master, &interval, master->dev->dev_addr);
  349. if (is_admin_up(master->dev))
  350. mod_timer(&hsr->announce_timer, jiffies + interval);
  351. rcu_read_unlock();
  352. }
  353. /* Announce (supervision frame) timer function for RedBox
  354. */
  355. static void hsr_proxy_announce(struct timer_list *t)
  356. {
  357. struct hsr_priv *hsr = timer_container_of(hsr, t,
  358. announce_proxy_timer);
  359. struct hsr_port *interlink;
  360. unsigned long interval = 0;
  361. struct hsr_node *node;
  362. rcu_read_lock();
  363. /* RedBOX sends supervisory frames to HSR network with MAC addresses
  364. * of SAN nodes stored in ProxyNodeTable.
  365. */
  366. interlink = hsr_port_get_hsr(hsr, HSR_PT_INTERLINK);
  367. if (!interlink)
  368. goto done;
  369. list_for_each_entry_rcu(node, &hsr->proxy_node_db, mac_list) {
  370. if (hsr_addr_is_redbox(hsr, node->macaddress_A))
  371. continue;
  372. hsr->proto_ops->send_sv_frame(interlink, &interval,
  373. node->macaddress_A);
  374. }
  375. if (is_admin_up(interlink->dev)) {
  376. if (!interval)
  377. interval = msecs_to_jiffies(HSR_ANNOUNCE_INTERVAL);
  378. mod_timer(&hsr->announce_proxy_timer, jiffies + interval);
  379. }
  380. done:
  381. rcu_read_unlock();
  382. }
  383. void hsr_del_ports(struct hsr_priv *hsr)
  384. {
  385. struct hsr_port *port;
  386. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_A);
  387. if (port)
  388. hsr_del_port(port);
  389. port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B);
  390. if (port)
  391. hsr_del_port(port);
  392. port = hsr_port_get_hsr(hsr, HSR_PT_INTERLINK);
  393. if (port)
  394. hsr_del_port(port);
  395. port = hsr_port_get_hsr(hsr, HSR_PT_MASTER);
  396. if (port)
  397. hsr_del_port(port);
  398. }
  399. static void hsr_set_rx_mode(struct net_device *dev)
  400. {
  401. struct hsr_port *port;
  402. struct hsr_priv *hsr;
  403. hsr = netdev_priv(dev);
  404. hsr_for_each_port_rtnl(hsr, port) {
  405. if (port->type == HSR_PT_MASTER)
  406. continue;
  407. switch (port->type) {
  408. case HSR_PT_SLAVE_A:
  409. case HSR_PT_SLAVE_B:
  410. dev_mc_sync_multiple(port->dev, dev);
  411. dev_uc_sync_multiple(port->dev, dev);
  412. break;
  413. default:
  414. break;
  415. }
  416. }
  417. }
  418. static void hsr_change_rx_flags(struct net_device *dev, int change)
  419. {
  420. struct hsr_port *port;
  421. struct hsr_priv *hsr;
  422. hsr = netdev_priv(dev);
  423. hsr_for_each_port_rtnl(hsr, port) {
  424. if (port->type == HSR_PT_MASTER)
  425. continue;
  426. switch (port->type) {
  427. case HSR_PT_SLAVE_A:
  428. case HSR_PT_SLAVE_B:
  429. if (change & IFF_ALLMULTI)
  430. dev_set_allmulti(port->dev,
  431. dev->flags &
  432. IFF_ALLMULTI ? 1 : -1);
  433. break;
  434. default:
  435. break;
  436. }
  437. }
  438. }
  439. static int hsr_ndo_vlan_rx_add_vid(struct net_device *dev,
  440. __be16 proto, u16 vid)
  441. {
  442. struct net_device *slave_a_dev = NULL;
  443. struct net_device *slave_b_dev = NULL;
  444. struct hsr_port *port;
  445. struct hsr_priv *hsr;
  446. int ret = 0;
  447. hsr = netdev_priv(dev);
  448. hsr_for_each_port_rtnl(hsr, port) {
  449. if (port->type == HSR_PT_MASTER ||
  450. port->type == HSR_PT_INTERLINK)
  451. continue;
  452. ret = vlan_vid_add(port->dev, proto, vid);
  453. switch (port->type) {
  454. case HSR_PT_SLAVE_A:
  455. if (ret) {
  456. netdev_err(dev, "add vid failed for Slave-A\n");
  457. goto unwind;
  458. }
  459. slave_a_dev = port->dev;
  460. break;
  461. case HSR_PT_SLAVE_B:
  462. if (ret) {
  463. netdev_err(dev, "add vid failed for Slave-B\n");
  464. goto unwind;
  465. }
  466. slave_b_dev = port->dev;
  467. break;
  468. default:
  469. if (ret)
  470. goto unwind;
  471. break;
  472. }
  473. }
  474. return 0;
  475. unwind:
  476. if (slave_a_dev)
  477. vlan_vid_del(slave_a_dev, proto, vid);
  478. if (slave_b_dev)
  479. vlan_vid_del(slave_b_dev, proto, vid);
  480. return ret;
  481. }
  482. static int hsr_ndo_vlan_rx_kill_vid(struct net_device *dev,
  483. __be16 proto, u16 vid)
  484. {
  485. struct hsr_port *port;
  486. struct hsr_priv *hsr;
  487. hsr = netdev_priv(dev);
  488. hsr_for_each_port_rtnl(hsr, port) {
  489. switch (port->type) {
  490. case HSR_PT_SLAVE_A:
  491. case HSR_PT_SLAVE_B:
  492. vlan_vid_del(port->dev, proto, vid);
  493. break;
  494. default:
  495. break;
  496. }
  497. }
  498. return 0;
  499. }
  500. static const struct net_device_ops hsr_device_ops = {
  501. .ndo_change_mtu = hsr_dev_change_mtu,
  502. .ndo_open = hsr_dev_open,
  503. .ndo_stop = hsr_dev_close,
  504. .ndo_start_xmit = hsr_dev_xmit,
  505. .ndo_change_rx_flags = hsr_change_rx_flags,
  506. .ndo_fix_features = hsr_fix_features,
  507. .ndo_set_rx_mode = hsr_set_rx_mode,
  508. .ndo_vlan_rx_add_vid = hsr_ndo_vlan_rx_add_vid,
  509. .ndo_vlan_rx_kill_vid = hsr_ndo_vlan_rx_kill_vid,
  510. };
  511. static const struct device_type hsr_type = {
  512. .name = "hsr",
  513. };
  514. static struct hsr_proto_ops hsr_ops = {
  515. .send_sv_frame = send_hsr_supervision_frame,
  516. .create_tagged_frame = hsr_create_tagged_frame,
  517. .get_untagged_frame = hsr_get_untagged_frame,
  518. .drop_frame = hsr_drop_frame,
  519. .fill_frame_info = hsr_fill_frame_info,
  520. .invalid_dan_ingress_frame = hsr_invalid_dan_ingress_frame,
  521. .register_frame_out = hsr_register_frame_out,
  522. };
  523. static struct hsr_proto_ops prp_ops = {
  524. .send_sv_frame = send_prp_supervision_frame,
  525. .create_tagged_frame = prp_create_tagged_frame,
  526. .get_untagged_frame = prp_get_untagged_frame,
  527. .drop_frame = prp_drop_frame,
  528. .fill_frame_info = prp_fill_frame_info,
  529. .handle_san_frame = prp_handle_san_frame,
  530. .update_san_info = prp_update_san_info,
  531. .register_frame_out = prp_register_frame_out,
  532. };
  533. void hsr_dev_setup(struct net_device *dev)
  534. {
  535. eth_hw_addr_random(dev);
  536. ether_setup(dev);
  537. dev->min_mtu = 0;
  538. dev->header_ops = &hsr_header_ops;
  539. dev->netdev_ops = &hsr_device_ops;
  540. SET_NETDEV_DEVTYPE(dev, &hsr_type);
  541. dev->priv_flags |= IFF_NO_QUEUE | IFF_DISABLE_NETPOLL;
  542. /* Prevent recursive tx locking */
  543. dev->lltx = true;
  544. /* Not sure about this. Taken from bridge code. netdevice.h says
  545. * it means "Does not change network namespaces".
  546. */
  547. dev->netns_immutable = true;
  548. dev->needs_free_netdev = true;
  549. dev->hw_features = NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA |
  550. NETIF_F_GSO_MASK | NETIF_F_HW_CSUM |
  551. NETIF_F_HW_VLAN_CTAG_TX |
  552. NETIF_F_HW_VLAN_CTAG_FILTER;
  553. dev->features = dev->hw_features;
  554. }
  555. /* Return true if dev is a HSR master; return false otherwise.
  556. */
  557. bool is_hsr_master(struct net_device *dev)
  558. {
  559. return (dev->netdev_ops->ndo_start_xmit == hsr_dev_xmit);
  560. }
  561. EXPORT_SYMBOL(is_hsr_master);
  562. struct net_device *hsr_get_port_ndev(struct net_device *ndev,
  563. enum hsr_port_type pt)
  564. {
  565. struct hsr_priv *hsr = netdev_priv(ndev);
  566. struct hsr_port *port;
  567. rcu_read_lock();
  568. hsr_for_each_port(hsr, port)
  569. if (port->type == pt) {
  570. dev_hold(port->dev);
  571. rcu_read_unlock();
  572. return port->dev;
  573. }
  574. rcu_read_unlock();
  575. return NULL;
  576. }
  577. EXPORT_SYMBOL(hsr_get_port_ndev);
  578. int hsr_get_port_type(struct net_device *hsr_dev, struct net_device *dev,
  579. enum hsr_port_type *type)
  580. {
  581. struct hsr_priv *hsr = netdev_priv(hsr_dev);
  582. struct hsr_port *port;
  583. rcu_read_lock();
  584. hsr_for_each_port(hsr, port) {
  585. if (port->dev == dev) {
  586. *type = port->type;
  587. rcu_read_unlock();
  588. return 0;
  589. }
  590. }
  591. rcu_read_unlock();
  592. return -EINVAL;
  593. }
  594. EXPORT_SYMBOL(hsr_get_port_type);
  595. /* Default multicast address for HSR Supervision frames */
  596. static const unsigned char def_multicast_addr[ETH_ALEN] __aligned(2) = {
  597. 0x01, 0x15, 0x4e, 0x00, 0x01, 0x00
  598. };
  599. int hsr_dev_finalize(struct net_device *hsr_dev, struct net_device *slave[2],
  600. struct net_device *interlink, unsigned char multicast_spec,
  601. u8 protocol_version, struct netlink_ext_ack *extack)
  602. {
  603. bool unregister = false;
  604. struct hsr_priv *hsr;
  605. int res;
  606. hsr = netdev_priv(hsr_dev);
  607. INIT_LIST_HEAD(&hsr->ports);
  608. INIT_LIST_HEAD(&hsr->node_db);
  609. INIT_LIST_HEAD(&hsr->proxy_node_db);
  610. spin_lock_init(&hsr->list_lock);
  611. eth_hw_addr_set(hsr_dev, slave[0]->dev_addr);
  612. /* initialize protocol specific functions */
  613. if (protocol_version == PRP_V1) {
  614. /* For PRP, lan_id has most significant 3 bits holding
  615. * the net_id of PRP_LAN_ID
  616. */
  617. hsr->net_id = PRP_LAN_ID << 1;
  618. hsr->proto_ops = &prp_ops;
  619. } else {
  620. hsr->proto_ops = &hsr_ops;
  621. }
  622. /* Make sure we recognize frames from ourselves in hsr_rcv() */
  623. res = hsr_create_self_node(hsr, hsr_dev->dev_addr,
  624. slave[1]->dev_addr);
  625. if (res < 0)
  626. return res;
  627. spin_lock_init(&hsr->seqnr_lock);
  628. /* Overflow soon to find bugs easier: */
  629. hsr->sequence_nr = HSR_SEQNR_START;
  630. hsr->sup_sequence_nr = HSR_SUP_SEQNR_START;
  631. timer_setup(&hsr->announce_timer, hsr_announce, 0);
  632. timer_setup(&hsr->prune_timer, hsr_prune_nodes, 0);
  633. timer_setup(&hsr->prune_proxy_timer, hsr_prune_proxy_nodes, 0);
  634. timer_setup(&hsr->announce_proxy_timer, hsr_proxy_announce, 0);
  635. ether_addr_copy(hsr->sup_multicast_addr, def_multicast_addr);
  636. hsr->sup_multicast_addr[ETH_ALEN - 1] = multicast_spec;
  637. hsr->prot_version = protocol_version;
  638. /* Make sure the 1st call to netif_carrier_on() gets through */
  639. netif_carrier_off(hsr_dev);
  640. res = hsr_add_port(hsr, hsr_dev, HSR_PT_MASTER, extack);
  641. if (res)
  642. goto err_add_master;
  643. /* HSR forwarding offload supported in lower device? */
  644. if ((slave[0]->features & NETIF_F_HW_HSR_FWD) &&
  645. (slave[1]->features & NETIF_F_HW_HSR_FWD))
  646. hsr->fwd_offloaded = true;
  647. if ((slave[0]->features & NETIF_F_HW_VLAN_CTAG_FILTER) &&
  648. (slave[1]->features & NETIF_F_HW_VLAN_CTAG_FILTER))
  649. hsr_dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  650. res = register_netdevice(hsr_dev);
  651. if (res)
  652. goto err_unregister;
  653. unregister = true;
  654. res = hsr_add_port(hsr, slave[0], HSR_PT_SLAVE_A, extack);
  655. if (res)
  656. goto err_unregister;
  657. res = hsr_add_port(hsr, slave[1], HSR_PT_SLAVE_B, extack);
  658. if (res)
  659. goto err_unregister;
  660. if (protocol_version == PRP_V1) {
  661. eth_hw_addr_set(slave[1], slave[0]->dev_addr);
  662. call_netdevice_notifiers(NETDEV_CHANGEADDR, slave[1]);
  663. }
  664. if (interlink) {
  665. res = hsr_add_port(hsr, interlink, HSR_PT_INTERLINK, extack);
  666. if (res)
  667. goto err_unregister;
  668. hsr->redbox = true;
  669. ether_addr_copy(hsr->macaddress_redbox, interlink->dev_addr);
  670. mod_timer(&hsr->prune_proxy_timer,
  671. jiffies + msecs_to_jiffies(PRUNE_PROXY_PERIOD));
  672. }
  673. hsr_debugfs_init(hsr, hsr_dev);
  674. mod_timer(&hsr->prune_timer, jiffies + msecs_to_jiffies(PRUNE_PERIOD));
  675. return 0;
  676. err_unregister:
  677. hsr_del_ports(hsr);
  678. err_add_master:
  679. hsr_del_self_node(hsr);
  680. if (unregister)
  681. unregister_netdevice(hsr_dev);
  682. return res;
  683. }