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#include <linux/kernel.h>
26#include <linux/gfp.h>
27#include <net/genetlink.h>
28#include <linux/nl802154.h>
29
30#include "ieee802154.h"
31
32static unsigned int ieee802154_seq_num;
33static DEFINE_SPINLOCK(ieee802154_seq_lock);
34
35struct genl_family nl802154_family = {
36 .id = GENL_ID_GENERATE,
37 .hdrsize = 0,
38 .name = IEEE802154_NL_NAME,
39 .version = 1,
40 .maxattr = IEEE802154_ATTR_MAX,
41};
42
43
44struct sk_buff *ieee802154_nl_create(int flags, u8 req)
45{
46 void *hdr;
47 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
48 unsigned long f;
49
50 if (!msg)
51 return NULL;
52
53 spin_lock_irqsave(&ieee802154_seq_lock, f);
54 hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
55 &nl802154_family, flags, req);
56 spin_unlock_irqrestore(&ieee802154_seq_lock, f);
57 if (!hdr) {
58 nlmsg_free(msg);
59 return NULL;
60 }
61
62 return msg;
63}
64
65int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
66{
67 struct nlmsghdr *nlh = nlmsg_hdr(msg);
68 void *hdr = genlmsg_data(nlmsg_data(nlh));
69
70 if (genlmsg_end(msg, hdr) < 0)
71 goto out;
72
73 return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
74out:
75 nlmsg_free(msg);
76 return -ENOBUFS;
77}
78
79struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
80 int flags, u8 req)
81{
82 void *hdr;
83 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
84
85 if (!msg)
86 return NULL;
87
88 hdr = genlmsg_put_reply(msg, info,
89 &nl802154_family, flags, req);
90 if (!hdr) {
91 nlmsg_free(msg);
92 return NULL;
93 }
94
95 return msg;
96}
97
98int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
99{
100 struct nlmsghdr *nlh = nlmsg_hdr(msg);
101 void *hdr = genlmsg_data(nlmsg_data(nlh));
102
103 if (genlmsg_end(msg, hdr) < 0)
104 goto out;
105
106 return genlmsg_reply(msg, info);
107out:
108 nlmsg_free(msg);
109 return -ENOBUFS;
110}
111
112static const struct genl_ops ieee8021154_ops[] = {
113
114 IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
115 ieee802154_dump_phy),
116 IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
117 IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
118
119 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
120 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
121 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
122 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
123 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
124 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
125 ieee802154_dump_iface),
126};
127
128static const struct genl_multicast_group ieee802154_mcgrps[] = {
129 [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, },
130 [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, },
131};
132
133
134int __init ieee802154_nl_init(void)
135{
136 return genl_register_family_with_ops_groups(&nl802154_family,
137 ieee8021154_ops,
138 ieee802154_mcgrps);
139}
140
141void __exit ieee802154_nl_exit(void)
142{
143 genl_unregister_family(&nl802154_family);
144}
145