Blame view

net/irda/irnetlink.c 3.19 KB
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
1
2
3
  /*
   * IrDA netlink layer, for stack configuration.
   *
22e1fb25e   Samuel Ortiz   [IrDA] Typo fix i...
4
   * Copyright (c) 2007 Samuel Ortiz <samuel@sortiz.org>
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
5
6
7
8
9
10
11
12
13
14
15
16
17
   *
   * Partly based on the 802.11 nelink implementation
   * (see net/wireless/nl80211.c) which is:
   * Copyright 2006 Johannes Berg <johannes@sipsolutions.net>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License version 2 as
   * published by the Free Software Foundation.
   *
   */
  
  #include <linux/socket.h>
  #include <linux/irda.h>
5a0e3ad6a   Tejun Heo   include cleanup: ...
18
  #include <linux/gfp.h>
881d966b4   Eric W. Biederman   [NET]: Make the d...
19
  #include <net/net_namespace.h>
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  #include <net/sock.h>
  #include <net/irda/irda.h>
  #include <net/irda/irlap.h>
  #include <net/genetlink.h>
  
  
  
  static struct genl_family irda_nl_family = {
  	.id = GENL_ID_GENERATE,
  	.name = IRDA_NL_NAME,
  	.hdrsize = 0,
  	.version = IRDA_NL_VERSION,
  	.maxattr = IRDA_NL_CMD_MAX,
  };
881d966b4   Eric W. Biederman   [NET]: Make the d...
34
  static struct net_device * ifname_to_netdev(struct net *net, struct genl_info *info)
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
35
36
37
38
39
40
41
  {
  	char * ifname;
  
  	if (!info->attrs[IRDA_NL_ATTR_IFNAME])
  		return NULL;
  
  	ifname = nla_data(info->attrs[IRDA_NL_ATTR_IFNAME]);
955a9d202   Joe Perches   irda: Convert IRD...
42
43
  	pr_debug("%s(): Looking for %s
  ", __func__, ifname);
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
44

881d966b4   Eric W. Biederman   [NET]: Make the d...
45
  	return dev_get_by_name(net, ifname);
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
46
47
48
49
50
51
52
53
54
55
56
57
  }
  
  static int irda_nl_set_mode(struct sk_buff *skb, struct genl_info *info)
  {
  	struct net_device * dev;
  	struct irlap_cb * irlap;
  	u32 mode;
  
  	if (!info->attrs[IRDA_NL_ATTR_MODE])
  		return -EINVAL;
  
  	mode = nla_get_u32(info->attrs[IRDA_NL_ATTR_MODE]);
955a9d202   Joe Perches   irda: Convert IRD...
58
59
  	pr_debug("%s(): Switching to mode: %d
  ", __func__, mode);
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
60

881d966b4   Eric W. Biederman   [NET]: Make the d...
61
  	dev = ifname_to_netdev(&init_net, info);
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  	if (!dev)
  		return -ENODEV;
  
  	irlap = (struct irlap_cb *)dev->atalk_ptr;
  	if (!irlap) {
  		dev_put(dev);
  		return -ENODEV;
  	}
  
  	irlap->mode = mode;
  
  	dev_put(dev);
  
  	return 0;
  }
  
  static int irda_nl_get_mode(struct sk_buff *skb, struct genl_info *info)
  {
  	struct net_device * dev;
  	struct irlap_cb * irlap;
  	struct sk_buff *msg;
  	void *hdr;
  	int ret = -ENOBUFS;
881d966b4   Eric W. Biederman   [NET]: Make the d...
85
  	dev = ifname_to_netdev(&init_net, info);
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
86
87
  	if (!dev)
  		return -ENODEV;
fd2120ca0   Pablo Neira Ayuso   net: use NLMSG_DE...
88
  	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
89
90
91
92
93
94
95
96
97
98
  	if (!msg) {
  		dev_put(dev);
  		return -ENOMEM;
  	}
  
  	irlap = (struct irlap_cb *)dev->atalk_ptr;
  	if (!irlap) {
  		ret = -ENODEV;
  		goto err_out;
  	}
15e473046   Eric W. Biederman   netlink: Rename p...
99
  	hdr = genlmsg_put(msg, info->snd_portid, info->snd_seq,
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
100
  			  &irda_nl_family, 0,  IRDA_NL_CMD_GET_MODE);
07035fc1b   Julius Volz   irda: Fix netlink...
101
102
  	if (hdr == NULL) {
  		ret = -EMSGSIZE;
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
103
104
105
106
  		goto err_out;
  	}
  
  	if(nla_put_string(msg, IRDA_NL_ATTR_IFNAME,
22117ea4f   Andy Whitcroft   [IRDA] irda_nl_ge...
107
  			  dev->name))
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
108
109
110
111
112
113
  		goto err_out;
  
  	if(nla_put_u32(msg, IRDA_NL_ATTR_MODE, irlap->mode))
  		goto err_out;
  
  	genlmsg_end(msg, hdr);
134e63756   Johannes Berg   genetlink: make n...
114
  	return genlmsg_reply(msg, info);
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
115
116
117
118
119
120
121
  
   err_out:
  	nlmsg_free(msg);
  	dev_put(dev);
  
  	return ret;
  }
b54452b07   Alexey Dobriyan   const: struct nla...
122
  static const struct nla_policy irda_nl_policy[IRDA_NL_ATTR_MAX + 1] = {
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
123
124
125
126
  	[IRDA_NL_ATTR_IFNAME] = { .type = NLA_NUL_STRING,
  				  .len = IFNAMSIZ-1 },
  	[IRDA_NL_ATTR_MODE] = { .type = NLA_U32 },
  };
4534de830   Johannes Berg   genetlink: make a...
127
  static const struct genl_ops irda_nl_ops[] = {
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  	{
  		.cmd = IRDA_NL_CMD_SET_MODE,
  		.doit = irda_nl_set_mode,
  		.policy = irda_nl_policy,
  		.flags = GENL_ADMIN_PERM,
  	},
  	{
  		.cmd = IRDA_NL_CMD_GET_MODE,
  		.doit = irda_nl_get_mode,
  		.policy = irda_nl_policy,
  		/* can be retrieved by unprivileged users */
  	},
  
  };
  
  int irda_nl_register(void)
  {
c53ed7423   Johannes Berg   genetlink: only p...
145
  	return genl_register_family_with_ops(&irda_nl_family, irda_nl_ops);
89da1ecf5   Samuel Ortiz   [IrDA]: Netlink l...
146
147
148
149
150
151
  }
  
  void irda_nl_unregister(void)
  {
  	genl_unregister_family(&irda_nl_family);
  }