aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--include/net/phonet/pn_dev.h1
-rw-r--r--net/phonet/pn_dev.c31
-rw-r--r--net/phonet/pn_netlink.c130
3 files changed, 162 insertions, 0 deletions
diff --git a/include/net/phonet/pn_dev.h b/include/net/phonet/pn_dev.h
index 87b5d8112e18..afa7defceb14 100644
--- a/include/net/phonet/pn_dev.h
+++ b/include/net/phonet/pn_dev.h
@@ -49,6 +49,7 @@ void phonet_address_notify(int event, struct net_device *dev, u8 addr);
49 49
50int phonet_route_add(struct net_device *dev, u8 daddr); 50int phonet_route_add(struct net_device *dev, u8 daddr);
51int phonet_route_del(struct net_device *dev, u8 daddr); 51int phonet_route_del(struct net_device *dev, u8 daddr);
52void rtm_phonet_notify(int event, struct net_device *dev, u8 dst);
52struct net_device *phonet_route_get(struct net *net, u8 daddr); 53struct net_device *phonet_route_get(struct net *net, u8 daddr);
53struct net_device *phonet_route_output(struct net *net, u8 daddr); 54struct net_device *phonet_route_output(struct net *net, u8 daddr);
54 55
diff --git a/net/phonet/pn_dev.c b/net/phonet/pn_dev.c
index 71fffa587783..6d64fda1afc9 100644
--- a/net/phonet/pn_dev.c
+++ b/net/phonet/pn_dev.c
@@ -240,6 +240,27 @@ static int phonet_device_autoconf(struct net_device *dev)
240 return 0; 240 return 0;
241} 241}
242 242
243static void phonet_route_autodel(struct net_device *dev)
244{
245 struct phonet_net *pnn = net_generic(dev_net(dev), phonet_net_id);
246 unsigned i;
247 DECLARE_BITMAP(deleted, 64);
248
249 /* Remove left-over Phonet routes */
250 bitmap_zero(deleted, 64);
251 spin_lock_bh(&pnn->routes.lock);
252 for (i = 0; i < 64; i++)
253 if (dev == pnn->routes.table[i]) {
254 set_bit(i, deleted);
255 pnn->routes.table[i] = NULL;
256 dev_put(dev);
257 }
258 spin_unlock_bh(&pnn->routes.lock);
259 for (i = find_first_bit(deleted, 64); i < 64;
260 i = find_next_bit(deleted, 64, i + 1))
261 rtm_phonet_notify(RTM_DELROUTE, dev, i);
262}
263
243/* notify Phonet of device events */ 264/* notify Phonet of device events */
244static int phonet_device_notify(struct notifier_block *me, unsigned long what, 265static int phonet_device_notify(struct notifier_block *me, unsigned long what,
245 void *arg) 266 void *arg)
@@ -253,6 +274,7 @@ static int phonet_device_notify(struct notifier_block *me, unsigned long what,
253 break; 274 break;
254 case NETDEV_UNREGISTER: 275 case NETDEV_UNREGISTER:
255 phonet_device_destroy(dev); 276 phonet_device_destroy(dev);
277 phonet_route_autodel(dev);
256 break; 278 break;
257 } 279 }
258 return 0; 280 return 0;
@@ -287,10 +309,19 @@ static void phonet_exit_net(struct net *net)
287{ 309{
288 struct phonet_net *pnn = net_generic(net, phonet_net_id); 310 struct phonet_net *pnn = net_generic(net, phonet_net_id);
289 struct net_device *dev; 311 struct net_device *dev;
312 unsigned i;
290 313
291 rtnl_lock(); 314 rtnl_lock();
292 for_each_netdev(net, dev) 315 for_each_netdev(net, dev)
293 phonet_device_destroy(dev); 316 phonet_device_destroy(dev);
317
318 for (i = 0; i < 64; i++) {
319 dev = pnn->routes.table[i];
320 if (dev) {
321 rtm_phonet_notify(RTM_DELROUTE, dev, i);
322 dev_put(dev);
323 }
324 }
294 rtnl_unlock(); 325 rtnl_unlock();
295 326
296 proc_net_remove(net, "phonet"); 327 proc_net_remove(net, "phonet");
diff --git a/net/phonet/pn_netlink.c b/net/phonet/pn_netlink.c
index d21fd3576610..d8f5d3fb9ee2 100644
--- a/net/phonet/pn_netlink.c
+++ b/net/phonet/pn_netlink.c
@@ -29,6 +29,8 @@
29#include <net/sock.h> 29#include <net/sock.h>
30#include <net/phonet/pn_dev.h> 30#include <net/phonet/pn_dev.h>
31 31
32/* Device address handling */
33
32static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr, 34static int fill_addr(struct sk_buff *skb, struct net_device *dev, u8 addr,
33 u32 pid, u32 seq, int event); 35 u32 pid, u32 seq, int event);
34 36
@@ -160,6 +162,131 @@ out:
160 return skb->len; 162 return skb->len;
161} 163}
162 164
165/* Routes handling */
166
167static int fill_route(struct sk_buff *skb, struct net_device *dev, u8 dst,
168 u32 pid, u32 seq, int event)
169{
170 struct rtmsg *rtm;
171 struct nlmsghdr *nlh;
172
173 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*rtm), 0);
174 if (nlh == NULL)
175 return -EMSGSIZE;
176
177 rtm = nlmsg_data(nlh);
178 rtm->rtm_family = AF_PHONET;
179 rtm->rtm_dst_len = 6;
180 rtm->rtm_src_len = 0;
181 rtm->rtm_tos = 0;
182 rtm->rtm_table = RT_TABLE_MAIN;
183 rtm->rtm_protocol = RTPROT_STATIC;
184 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
185 rtm->rtm_type = RTN_UNICAST;
186 rtm->rtm_flags = 0;
187 NLA_PUT_U8(skb, RTA_DST, dst);
188 NLA_PUT_U32(skb, RTA_OIF, dev->ifindex);
189 return nlmsg_end(skb, nlh);
190
191nla_put_failure:
192 nlmsg_cancel(skb, nlh);
193 return -EMSGSIZE;
194}
195
196void rtm_phonet_notify(int event, struct net_device *dev, u8 dst)
197{
198 struct sk_buff *skb;
199 int err = -ENOBUFS;
200
201 skb = nlmsg_new(NLMSG_ALIGN(sizeof(struct ifaddrmsg)) +
202 nla_total_size(1) + nla_total_size(4), GFP_KERNEL);
203 if (skb == NULL)
204 goto errout;
205 err = fill_route(skb, dev, dst, 0, 0, event);
206 if (err < 0) {
207 WARN_ON(err == -EMSGSIZE);
208 kfree_skb(skb);
209 goto errout;
210 }
211 rtnl_notify(skb, dev_net(dev), 0,
212 RTNLGRP_PHONET_ROUTE, NULL, GFP_KERNEL);
213 return;
214errout:
215 if (err < 0)
216 rtnl_set_sk_err(dev_net(dev), RTNLGRP_PHONET_ROUTE, err);
217}
218
219static const struct nla_policy rtm_phonet_policy[RTA_MAX+1] = {
220 [RTA_DST] = { .type = NLA_U8 },
221 [RTA_OIF] = { .type = NLA_U32 },
222};
223
224static int route_doit(struct sk_buff *skb, struct nlmsghdr *nlh, void *attr)
225{
226 struct net *net = sock_net(skb->sk);
227 struct nlattr *tb[RTA_MAX+1];
228 struct net_device *dev;
229 struct rtmsg *rtm;
230 int err;
231 u8 dst;
232
233 if (!capable(CAP_SYS_ADMIN))
234 return -EPERM;
235
236 ASSERT_RTNL();
237
238 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX, rtm_phonet_policy);
239 if (err < 0)
240 return err;
241
242 rtm = nlmsg_data(nlh);
243 if (rtm->rtm_table != RT_TABLE_MAIN || rtm->rtm_type != RTN_UNICAST)
244 return -EINVAL;
245 if (tb[RTA_DST] == NULL || tb[RTA_OIF] == NULL)
246 return -EINVAL;
247 dst = nla_get_u8(tb[RTA_DST]);
248 if (dst & 3) /* Phonet addresses only have 6 high-order bits */
249 return -EINVAL;
250
251 dev = __dev_get_by_index(net, nla_get_u32(tb[RTA_OIF]));
252 if (dev == NULL)
253 return -ENODEV;
254
255 if (nlh->nlmsg_type == RTM_NEWROUTE)
256 err = phonet_route_add(dev, dst);
257 else
258 err = phonet_route_del(dev, dst);
259 if (!err)
260 rtm_phonet_notify(nlh->nlmsg_type, dev, dst);
261 return err;
262}
263
264static int route_dumpit(struct sk_buff *skb, struct netlink_callback *cb)
265{
266 struct net *net = sock_net(skb->sk);
267 u8 addr, addr_idx = 0, addr_start_idx = cb->args[0];
268
269 for (addr = 0; addr < 64; addr++) {
270 struct net_device *dev;
271
272 dev = phonet_route_get(net, addr << 2);
273 if (!dev)
274 continue;
275
276 if (addr_idx++ < addr_start_idx)
277 continue;
278 if (fill_route(skb, dev, addr << 2, NETLINK_CB(cb->skb).pid,
279 cb->nlh->nlmsg_seq, RTM_NEWROUTE))
280 goto out;
281 }
282
283out:
284 cb->args[0] = addr_idx;
285 cb->args[1] = 0;
286
287 return skb->len;
288}
289
163int __init phonet_netlink_register(void) 290int __init phonet_netlink_register(void)
164{ 291{
165 int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit, NULL); 292 int err = __rtnl_register(PF_PHONET, RTM_NEWADDR, addr_doit, NULL);
@@ -169,5 +296,8 @@ int __init phonet_netlink_register(void)
169 /* Further __rtnl_register() cannot fail */ 296 /* Further __rtnl_register() cannot fail */
170 __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL); 297 __rtnl_register(PF_PHONET, RTM_DELADDR, addr_doit, NULL);
171 __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit); 298 __rtnl_register(PF_PHONET, RTM_GETADDR, NULL, getaddr_dumpit);
299 __rtnl_register(PF_PHONET, RTM_NEWROUTE, route_doit, NULL);
300 __rtnl_register(PF_PHONET, RTM_DELROUTE, route_doit, NULL);
301 __rtnl_register(PF_PHONET, RTM_GETROUTE, NULL, route_dumpit);
172 return 0; 302 return 0;
173} 303}