2 * IrDA netlink layer, for stack configuration.
4 * Copyright (c) 2007 Samuel Ortiz <samuel@sortiz.org>
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>
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.
16 #include <linux/socket.h>
17 #include <linux/irda.h>
18 #include <net/net_namespace.h>
20 #include <net/irda/irda.h>
21 #include <net/irda/irlap.h>
22 #include <net/genetlink.h>
26 static struct genl_family irda_nl_family = {
27 .id = GENL_ID_GENERATE,
30 .version = IRDA_NL_VERSION,
31 .maxattr = IRDA_NL_CMD_MAX,
34 static struct net_device * ifname_to_netdev(struct net *net, struct genl_info *info)
38 if (!info->attrs[IRDA_NL_ATTR_IFNAME])
41 ifname = nla_data(info->attrs[IRDA_NL_ATTR_IFNAME]);
43 IRDA_DEBUG(5, "%s(): Looking for %s\n", __func__, ifname);
45 return dev_get_by_name(net, ifname);
48 static int irda_nl_set_mode(struct sk_buff *skb, struct genl_info *info)
50 struct net_device * dev;
51 struct irlap_cb * irlap;
54 if (!info->attrs[IRDA_NL_ATTR_MODE])
57 mode = nla_get_u32(info->attrs[IRDA_NL_ATTR_MODE]);
59 IRDA_DEBUG(5, "%s(): Switching to mode: %d\n", __func__, mode);
61 dev = ifname_to_netdev(&init_net, info);
65 irlap = (struct irlap_cb *)dev->atalk_ptr;
78 static int irda_nl_get_mode(struct sk_buff *skb, struct genl_info *info)
80 struct net_device * dev;
81 struct irlap_cb * irlap;
86 dev = ifname_to_netdev(&init_net, info);
90 msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
96 irlap = (struct irlap_cb *)dev->atalk_ptr;
102 hdr = genlmsg_put(msg, info->snd_pid, info->snd_seq,
103 &irda_nl_family, 0, IRDA_NL_CMD_GET_MODE);
109 if(nla_put_string(msg, IRDA_NL_ATTR_IFNAME,
113 if(nla_put_u32(msg, IRDA_NL_ATTR_MODE, irlap->mode))
116 genlmsg_end(msg, hdr);
118 return genlmsg_unicast(msg, info->snd_pid);
127 static struct nla_policy irda_nl_policy[IRDA_NL_ATTR_MAX + 1] = {
128 [IRDA_NL_ATTR_IFNAME] = { .type = NLA_NUL_STRING,
130 [IRDA_NL_ATTR_MODE] = { .type = NLA_U32 },
133 static struct genl_ops irda_nl_ops[] = {
135 .cmd = IRDA_NL_CMD_SET_MODE,
136 .doit = irda_nl_set_mode,
137 .policy = irda_nl_policy,
138 .flags = GENL_ADMIN_PERM,
141 .cmd = IRDA_NL_CMD_GET_MODE,
142 .doit = irda_nl_get_mode,
143 .policy = irda_nl_policy,
144 /* can be retrieved by unprivileged users */
149 int irda_nl_register(void)
153 err = genl_register_family(&irda_nl_family);
157 for (i = 0; i < ARRAY_SIZE(irda_nl_ops); i++) {
158 err = genl_register_ops(&irda_nl_family, &irda_nl_ops[i]);
164 genl_unregister_family(&irda_nl_family);
168 void irda_nl_unregister(void)
170 genl_unregister_family(&irda_nl_family);