blob: 7fc340e574cf3b1e5484cbcbabe3a1e33be582b4 [file] [log] [blame]
Samuel Ortiz89da1ec2007-07-02 22:54:18 -07001/*
2 * IrDA netlink layer, for stack configuration.
3 *
Samuel Ortiz22e1fb22007-07-21 19:06:53 -07004 * Copyright (c) 2007 Samuel Ortiz <samuel@sortiz.org>
Samuel Ortiz89da1ec2007-07-02 22:54:18 -07005 *
6 * Partly based on the 802.11 nelink implementation
7 * (see net/wireless/nl80211.c) which is:
8 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 *
14 */
15
16#include <linux/socket.h>
17#include <linux/irda.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/gfp.h>
Eric W. Biederman881d9662007-09-17 11:56:21 -070019#include <net/net_namespace.h>
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070020#include <net/sock.h>
21#include <net/irda/irda.h>
22#include <net/irda/irlap.h>
23#include <net/genetlink.h>
24
25
26
Johannes Berg489111e2016-10-24 14:40:03 +020027static struct genl_family irda_nl_family;
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070028
Eric W. Biederman881d9662007-09-17 11:56:21 -070029static struct net_device * ifname_to_netdev(struct net *net, struct genl_info *info)
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070030{
31 char * ifname;
32
33 if (!info->attrs[IRDA_NL_ATTR_IFNAME])
34 return NULL;
35
36 ifname = nla_data(info->attrs[IRDA_NL_ATTR_IFNAME]);
37
Joe Perches955a9d202014-11-11 14:44:57 -080038 pr_debug("%s(): Looking for %s\n", __func__, ifname);
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070039
Eric W. Biederman881d9662007-09-17 11:56:21 -070040 return dev_get_by_name(net, ifname);
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070041}
42
43static int irda_nl_set_mode(struct sk_buff *skb, struct genl_info *info)
44{
45 struct net_device * dev;
46 struct irlap_cb * irlap;
47 u32 mode;
48
49 if (!info->attrs[IRDA_NL_ATTR_MODE])
50 return -EINVAL;
51
52 mode = nla_get_u32(info->attrs[IRDA_NL_ATTR_MODE]);
53
Joe Perches955a9d202014-11-11 14:44:57 -080054 pr_debug("%s(): Switching to mode: %d\n", __func__, mode);
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070055
Eric W. Biederman881d9662007-09-17 11:56:21 -070056 dev = ifname_to_netdev(&init_net, info);
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070057 if (!dev)
58 return -ENODEV;
59
60 irlap = (struct irlap_cb *)dev->atalk_ptr;
61 if (!irlap) {
62 dev_put(dev);
63 return -ENODEV;
64 }
65
66 irlap->mode = mode;
67
68 dev_put(dev);
69
70 return 0;
71}
72
73static int irda_nl_get_mode(struct sk_buff *skb, struct genl_info *info)
74{
75 struct net_device * dev;
76 struct irlap_cb * irlap;
77 struct sk_buff *msg;
78 void *hdr;
79 int ret = -ENOBUFS;
80
Eric W. Biederman881d9662007-09-17 11:56:21 -070081 dev = ifname_to_netdev(&init_net, info);
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070082 if (!dev)
83 return -ENODEV;
84
Pablo Neira Ayusofd2120c2009-05-19 15:27:55 -070085 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070086 if (!msg) {
87 dev_put(dev);
88 return -ENOMEM;
89 }
90
91 irlap = (struct irlap_cb *)dev->atalk_ptr;
92 if (!irlap) {
93 ret = -ENODEV;
94 goto err_out;
95 }
96
Eric W. Biederman15e47302012-09-07 20:12:54 +000097 hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
Samuel Ortiz89da1ec2007-07-02 22:54:18 -070098 &irda_nl_family, 0, IRDA_NL_CMD_GET_MODE);
Julius Volz07035fc2008-07-08 03:07:43 -070099 if (hdr == NULL) {
100 ret = -EMSGSIZE;
Samuel Ortiz89da1ec2007-07-02 22:54:18 -0700101 goto err_out;
102 }
103
104 if(nla_put_string(msg, IRDA_NL_ATTR_IFNAME,
Andy Whitcroft22117ea2007-08-21 21:23:39 -0700105 dev->name))
Samuel Ortiz89da1ec2007-07-02 22:54:18 -0700106 goto err_out;
107
108 if(nla_put_u32(msg, IRDA_NL_ATTR_MODE, irlap->mode))
109 goto err_out;
110
111 genlmsg_end(msg, hdr);
112
Johannes Berg134e6372009-07-10 09:51:34 +0000113 return genlmsg_reply(msg, info);
Samuel Ortiz89da1ec2007-07-02 22:54:18 -0700114
115 err_out:
116 nlmsg_free(msg);
117 dev_put(dev);
118
119 return ret;
120}
121
Alexey Dobriyanb54452b2010-02-18 08:14:31 +0000122static const struct nla_policy irda_nl_policy[IRDA_NL_ATTR_MAX + 1] = {
Samuel Ortiz89da1ec2007-07-02 22:54:18 -0700123 [IRDA_NL_ATTR_IFNAME] = { .type = NLA_NUL_STRING,
124 .len = IFNAMSIZ-1 },
125 [IRDA_NL_ATTR_MODE] = { .type = NLA_U32 },
126};
127
Johannes Berg4534de82013-11-14 17:14:46 +0100128static const struct genl_ops irda_nl_ops[] = {
Samuel Ortiz89da1ec2007-07-02 22:54:18 -0700129 {
130 .cmd = IRDA_NL_CMD_SET_MODE,
131 .doit = irda_nl_set_mode,
132 .policy = irda_nl_policy,
133 .flags = GENL_ADMIN_PERM,
134 },
135 {
136 .cmd = IRDA_NL_CMD_GET_MODE,
137 .doit = irda_nl_get_mode,
138 .policy = irda_nl_policy,
139 /* can be retrieved by unprivileged users */
140 },
141
142};
143
Johannes Berg56989f62016-10-24 14:40:05 +0200144static struct genl_family irda_nl_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200145 .name = IRDA_NL_NAME,
146 .hdrsize = 0,
147 .version = IRDA_NL_VERSION,
148 .maxattr = IRDA_NL_CMD_MAX,
149 .module = THIS_MODULE,
150 .ops = irda_nl_ops,
151 .n_ops = ARRAY_SIZE(irda_nl_ops),
152};
153
Johannes Berg56989f62016-10-24 14:40:05 +0200154int __init irda_nl_register(void)
Samuel Ortiz89da1ec2007-07-02 22:54:18 -0700155{
Johannes Berg489111e2016-10-24 14:40:03 +0200156 return genl_register_family(&irda_nl_family);
Samuel Ortiz89da1ec2007-07-02 22:54:18 -0700157}
158
159void irda_nl_unregister(void)
160{
161 genl_unregister_family(&irda_nl_family);
162}