1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/netdevice.h>
34#include <linux/if_arp.h>
35#include <linux/module.h>
36#include <net/rtnetlink.h>
37#include "ipoib.h"
38
39static const struct nla_policy ipoib_policy[IFLA_IPOIB_MAX + 1] = {
40 [IFLA_IPOIB_PKEY] = { .type = NLA_U16 },
41 [IFLA_IPOIB_MODE] = { .type = NLA_U16 },
42 [IFLA_IPOIB_UMCAST] = { .type = NLA_U16 },
43};
44
45static int ipoib_fill_info(struct sk_buff *skb, const struct net_device *dev)
46{
47 struct ipoib_dev_priv *priv = ipoib_priv(dev);
48 u16 val;
49
50 if (nla_put_u16(skb, IFLA_IPOIB_PKEY, priv->pkey))
51 goto nla_put_failure;
52
53 val = test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
54 if (nla_put_u16(skb, IFLA_IPOIB_MODE, val))
55 goto nla_put_failure;
56
57 val = test_bit(IPOIB_FLAG_UMCAST, &priv->flags);
58 if (nla_put_u16(skb, IFLA_IPOIB_UMCAST, val))
59 goto nla_put_failure;
60
61 return 0;
62
63nla_put_failure:
64 return -EMSGSIZE;
65}
66
67static int ipoib_changelink(struct net_device *dev, struct nlattr *tb[],
68 struct nlattr *data[],
69 struct netlink_ext_ack *extack)
70{
71 u16 mode, umcast;
72 int ret = 0;
73
74 if (data[IFLA_IPOIB_MODE]) {
75 mode = nla_get_u16(data[IFLA_IPOIB_MODE]);
76 if (mode == IPOIB_MODE_DATAGRAM)
77 ret = ipoib_set_mode(dev, "datagram\n");
78 else if (mode == IPOIB_MODE_CONNECTED)
79 ret = ipoib_set_mode(dev, "connected\n");
80 else
81 ret = -EINVAL;
82
83 if (ret < 0)
84 goto out_err;
85 }
86
87 if (data[IFLA_IPOIB_UMCAST]) {
88 umcast = nla_get_u16(data[IFLA_IPOIB_UMCAST]);
89 ipoib_set_umcast(dev, umcast);
90 }
91
92out_err:
93 return ret;
94}
95
96static int ipoib_new_child_link(struct net *src_net, struct net_device *dev,
97 struct nlattr *tb[], struct nlattr *data[],
98 struct netlink_ext_ack *extack)
99{
100 struct net_device *pdev;
101 struct ipoib_dev_priv *ppriv;
102 u16 child_pkey;
103 int err;
104
105 if (!tb[IFLA_LINK])
106 return -EINVAL;
107
108 pdev = __dev_get_by_index(src_net, nla_get_u32(tb[IFLA_LINK]));
109 if (!pdev || pdev->type != ARPHRD_INFINIBAND)
110 return -ENODEV;
111
112 ppriv = ipoib_priv(pdev);
113
114 if (test_bit(IPOIB_FLAG_SUBINTERFACE, &ppriv->flags)) {
115 ipoib_warn(ppriv, "child creation disallowed for child devices\n");
116 return -EINVAL;
117 }
118
119 if (!data || !data[IFLA_IPOIB_PKEY]) {
120 ipoib_dbg(ppriv, "no pkey specified, using parent pkey\n");
121 child_pkey = ppriv->pkey;
122 } else
123 child_pkey = nla_get_u16(data[IFLA_IPOIB_PKEY]);
124
125 if (child_pkey == 0 || child_pkey == 0x8000)
126 return -EINVAL;
127
128
129
130
131
132 child_pkey |= 0x8000;
133
134 err = __ipoib_vlan_add(ppriv, ipoib_priv(dev),
135 child_pkey, IPOIB_RTNL_CHILD);
136
137 if (!err && data)
138 err = ipoib_changelink(dev, tb, data, extack);
139 return err;
140}
141
142static void ipoib_unregister_child_dev(struct net_device *dev, struct list_head *head)
143{
144 struct ipoib_dev_priv *priv, *ppriv;
145
146 priv = ipoib_priv(dev);
147 ppriv = ipoib_priv(priv->parent);
148
149 down_write(&ppriv->vlan_rwsem);
150 unregister_netdevice_queue(dev, head);
151 list_del(&priv->list);
152 up_write(&ppriv->vlan_rwsem);
153}
154
155static size_t ipoib_get_size(const struct net_device *dev)
156{
157 return nla_total_size(2) +
158 nla_total_size(2) +
159 nla_total_size(2);
160}
161
162static struct rtnl_link_ops ipoib_link_ops __read_mostly = {
163 .kind = "ipoib",
164 .maxtype = IFLA_IPOIB_MAX,
165 .policy = ipoib_policy,
166 .priv_size = sizeof(struct ipoib_dev_priv),
167 .setup = ipoib_setup_common,
168 .newlink = ipoib_new_child_link,
169 .changelink = ipoib_changelink,
170 .dellink = ipoib_unregister_child_dev,
171 .get_size = ipoib_get_size,
172 .fill_info = ipoib_fill_info,
173};
174
175int __init ipoib_netlink_init(void)
176{
177 return rtnl_link_register(&ipoib_link_ops);
178}
179
180void __exit ipoib_netlink_fini(void)
181{
182 rtnl_link_unregister(&ipoib_link_ops);
183}
184
185MODULE_ALIAS_RTNL_LINK("ipoib");
186