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 */ 7 8 #include <linux/netdevice.h> 9 #include <linux/rculist.h> 10 #include <linux/timer.h> 11 #include <linux/etherdevice.h> 12 #include "hsr_main.h" 13 #include "hsr_device.h" 14 #include "hsr_netlink.h" 15 #include "hsr_framereg.h" 16 #include "hsr_slave.h" 17 18 static bool hsr_slave_empty(struct hsr_priv *hsr) 19 { 20 struct hsr_port *port; 21 22 hsr_for_each_port(hsr, port) 23 if (port->type != HSR_PT_MASTER) 24 return false; 25 return true; 26 } 27 28 static int hsr_netdev_notify(struct notifier_block *nb, unsigned long event, 29 void *ptr) 30 { 31 struct hsr_port *port, *master; 32 struct net_device *dev; 33 struct hsr_priv *hsr; 34 LIST_HEAD(list_kill); 35 int mtu_max; 36 int res; 37 38 dev = netdev_notifier_info_to_dev(ptr); 39 port = hsr_port_get_rtnl(dev); 40 if (!port) { 41 if (!is_hsr_master(dev)) 42 return NOTIFY_DONE; /* Not an HSR device */ 43 hsr = netdev_priv(dev); 44 port = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 45 if (!port) { 46 /* Resend of notification concerning removed device? */ 47 return NOTIFY_DONE; 48 } 49 } else { 50 hsr = port->hsr; 51 } 52 53 switch (event) { 54 case NETDEV_UP: /* Administrative state DOWN */ 55 case NETDEV_DOWN: /* Administrative state UP */ 56 case NETDEV_CHANGE: /* Link (carrier) state changes */ 57 hsr_check_carrier_and_operstate(hsr); 58 break; 59 case NETDEV_CHANGENAME: 60 if (is_hsr_master(dev)) 61 hsr_debugfs_rename(dev); 62 break; 63 case NETDEV_CHANGEADDR: 64 if (port->type == HSR_PT_MASTER) { 65 /* This should not happen since there's no 66 * ndo_set_mac_address() for HSR devices - i.e. not 67 * supported. 68 */ 69 break; 70 } 71 72 master = hsr_port_get_hsr(hsr, HSR_PT_MASTER); 73 74 if (port->type == HSR_PT_SLAVE_A) { 75 ether_addr_copy(master->dev->dev_addr, dev->dev_addr); 76 call_netdevice_notifiers(NETDEV_CHANGEADDR, 77 master->dev); 78 } 79 80 /* Make sure we recognize frames from ourselves in hsr_rcv() */ 81 port = hsr_port_get_hsr(hsr, HSR_PT_SLAVE_B); 82 res = hsr_create_self_node(hsr, 83 master->dev->dev_addr, 84 port ? 85 port->dev->dev_addr : 86 master->dev->dev_addr); 87 if (res) 88 netdev_warn(master->dev, 89 "Could not update HSR node address.\n"); 90 break; 91 case NETDEV_CHANGEMTU: 92 if (port->type == HSR_PT_MASTER) 93 break; /* Handled in ndo_change_mtu() */ 94 mtu_max = hsr_get_max_mtu(port->hsr); 95 master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER); 96 master->dev->mtu = mtu_max; 97 break; 98 case NETDEV_UNREGISTER: 99 if (!is_hsr_master(dev)) { 100 master = hsr_port_get_hsr(port->hsr, HSR_PT_MASTER); 101 hsr_del_port(port); 102 if (hsr_slave_empty(master->hsr)) { 103 unregister_netdevice_queue(master->dev, 104 &list_kill); 105 unregister_netdevice_many(&list_kill); 106 } 107 } 108 break; 109 case NETDEV_PRE_TYPE_CHANGE: 110 /* HSR works only on Ethernet devices. Refuse slave to change 111 * its type. 112 */ 113 return NOTIFY_BAD; 114 } 115 116 return NOTIFY_DONE; 117 } 118 119 struct hsr_port *hsr_port_get_hsr(struct hsr_priv *hsr, enum hsr_port_type pt) 120 { 121 struct hsr_port *port; 122 123 hsr_for_each_port(hsr, port) 124 if (port->type == pt) 125 return port; 126 return NULL; 127 } 128 129 static struct notifier_block hsr_nb = { 130 .notifier_call = hsr_netdev_notify, /* Slave event notifications */ 131 }; 132 133 static int __init hsr_init(void) 134 { 135 int res; 136 137 BUILD_BUG_ON(sizeof(struct hsr_tag) != HSR_HLEN); 138 139 register_netdevice_notifier(&hsr_nb); 140 res = hsr_netlink_init(); 141 142 return res; 143 } 144 145 static void __exit hsr_exit(void) 146 { 147 unregister_netdevice_notifier(&hsr_nb); 148 hsr_netlink_exit(); 149 hsr_debugfs_remove_root(); 150 } 151 152 module_init(hsr_init); 153 module_exit(hsr_exit); 154 MODULE_LICENSE("GPL"); 155