OSDN Git Service

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