libnl  3.2.7
fw.c
1 /*
2  * lib/route/cls/fw.c fw classifier
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2011 Thomas Graf <tgraf@suug.ch>
10  * Copyright (c) 2006 Petr Gotthard <petr.gotthard@siemens.com>
11  * Copyright (c) 2006 Siemens AG Oesterreich
12  */
13 
14 /**
15  * @ingroup cls
16  * @defgroup cls_fw Firewall Classifier
17  *
18  * @{
19  */
20 
21 #include <netlink-local.h>
22 #include <netlink-tc.h>
23 #include <netlink/netlink.h>
24 #include <netlink/route/tc-api.h>
25 #include <netlink/route/classifier.h>
26 #include <netlink/route/cls/fw.h>
27 
28 /** @cond SKIP */
29 #define FW_ATTR_CLASSID 0x001
30 #define FW_ATTR_ACTION 0x002
31 #define FW_ATTR_POLICE 0x004
32 #define FW_ATTR_INDEV 0x008
33 /** @endcond */
34 
35 static struct nla_policy fw_policy[TCA_FW_MAX+1] = {
36  [TCA_FW_CLASSID] = { .type = NLA_U32 },
37  [TCA_FW_INDEV] = { .type = NLA_STRING,
38  .maxlen = IFNAMSIZ },
39 };
40 
41 static int fw_msg_parser(struct rtnl_tc *tc, void *data)
42 {
43  struct nlattr *tb[TCA_FW_MAX + 1];
44  struct rtnl_fw *f = data;
45  int err;
46 
47  err = tca_parse(tb, TCA_FW_MAX, tc, fw_policy);
48  if (err < 0)
49  return err;
50 
51  if (tb[TCA_FW_CLASSID]) {
52  f->cf_classid = nla_get_u32(tb[TCA_FW_CLASSID]);
53  f->cf_mask |= FW_ATTR_CLASSID;
54  }
55 
56  if (tb[TCA_FW_ACT]) {
57  f->cf_act = nl_data_alloc_attr(tb[TCA_FW_ACT]);
58  if (!f->cf_act)
59  return -NLE_NOMEM;
60  f->cf_mask |= FW_ATTR_ACTION;
61  }
62 
63  if (tb[TCA_FW_POLICE]) {
64  f->cf_police = nl_data_alloc_attr(tb[TCA_FW_POLICE]);
65  if (!f->cf_police)
66  return -NLE_NOMEM;
67  f->cf_mask |= FW_ATTR_POLICE;
68  }
69 
70  if (tb[TCA_FW_INDEV]) {
71  nla_strlcpy(f->cf_indev, tb[TCA_FW_INDEV], IFNAMSIZ);
72  f->cf_mask |= FW_ATTR_INDEV;
73  }
74 
75  return 0;
76 }
77 
78 static void fw_free_data(struct rtnl_tc *tc, void *data)
79 {
80  struct rtnl_fw *f = data;
81 
82  nl_data_free(f->cf_act);
83  nl_data_free(f->cf_police);
84 }
85 
86 static int fw_clone(void *_dst, void *_src)
87 {
88  struct rtnl_fw *dst = _dst, *src = _src;
89 
90  if (src->cf_act && !(dst->cf_act = nl_data_clone(src->cf_act)))
91  return -NLE_NOMEM;
92 
93  if (src->cf_police && !(dst->cf_police = nl_data_clone(src->cf_police)))
94  return -NLE_NOMEM;
95 
96  return 0;
97 }
98 
99 static void fw_dump_line(struct rtnl_tc *tc, void *data,
100  struct nl_dump_params *p)
101 {
102  struct rtnl_fw *f = data;
103 
104  if (f && f->cf_mask & FW_ATTR_CLASSID) {
105  char buf[32];
106 
107  nl_dump(p, " target %s",
108  rtnl_tc_handle2str(f->cf_classid, buf, sizeof(buf)));
109  }
110 }
111 
112 static void fw_dump_details(struct rtnl_tc *tc, void *data,
113  struct nl_dump_params *p)
114 {
115  struct rtnl_fw *f = data;
116 
117  if (f && f->cf_mask & FW_ATTR_INDEV)
118  nl_dump(p, "indev %s ", f->cf_indev);
119 }
120 
121 static int fw_msg_fill(struct rtnl_tc *tc, void *data, struct nl_msg *msg)
122 {
123  struct rtnl_fw *f = data;
124 
125  if (!f)
126  return 0;
127 
128  if (f->cf_mask & FW_ATTR_CLASSID)
129  NLA_PUT_U32(msg, TCA_FW_CLASSID, f->cf_classid);
130 
131  if (f->cf_mask & FW_ATTR_ACTION)
132  NLA_PUT_DATA(msg, TCA_FW_ACT, f->cf_act);
133 
134  if (f->cf_mask & FW_ATTR_POLICE)
135  NLA_PUT_DATA(msg, TCA_FW_POLICE, f->cf_police);
136 
137  if (f->cf_mask & FW_ATTR_INDEV)
138  NLA_PUT_STRING(msg, TCA_FW_INDEV, f->cf_indev);
139 
140  return 0;
141 
142 nla_put_failure:
143  return -NLE_MSGSIZE;
144 }
145 
146 /**
147  * @name Attribute Modifications
148  * @{
149  */
150 
151 int rtnl_fw_set_classid(struct rtnl_cls *cls, uint32_t classid)
152 {
153  struct rtnl_fw *f;
154 
155  if (!(f = rtnl_tc_data(TC_CAST(cls))))
156  return -NLE_NOMEM;
157 
158  f->cf_classid = classid;
159  f->cf_mask |= FW_ATTR_CLASSID;
160 
161  return 0;
162 }
163 
164 /** @} */
165 
166 static struct rtnl_tc_ops fw_ops = {
167  .to_kind = "fw",
168  .to_type = RTNL_TC_TYPE_CLS,
169  .to_size = sizeof(struct rtnl_fw),
170  .to_msg_parser = fw_msg_parser,
171  .to_msg_fill = fw_msg_fill,
172  .to_free_data = fw_free_data,
173  .to_clone = fw_clone,
174  .to_dump = {
175  [NL_DUMP_LINE] = fw_dump_line,
176  [NL_DUMP_DETAILS] = fw_dump_details,
177  },
178 };
179 
180 static void __init fw_init(void)
181 {
182  rtnl_tc_register(&fw_ops);
183 }
184 
185 static void __exit fw_exit(void)
186 {
187  rtnl_tc_unregister(&fw_ops);
188 }
189 
190 /** @} */