libnl  3.2.24-rc1
pktloc.c
1 /*
2  * lib/route/pktloc.c Packet Location Aliasing
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) 2008-2013 Thomas Graf <tgraf@suug.ch>
10  */
11 
12 /**
13  * @ingroup tc
14  * @defgroup pktloc Packet Location Aliasing
15  * Packet Location Aliasing
16  *
17  * The packet location aliasing interface eases the use of offset definitions
18  * inside packets by allowing them to be referenced by name. Known positions
19  * of protocol fields are stored in a configuration file and associated with
20  * a name for later reference. The configuration file is distributed with the
21  * library and provides a well defined set of definitions for most common
22  * protocol fields.
23  *
24  * @section pktloc_examples Examples
25  * @par Example 1.1 Looking up a packet location
26  * @code
27  * struct rtnl_pktloc *loc;
28  *
29  * rtnl_pktloc_lookup("ip.src", &loc);
30  * @endcode
31  * @{
32  */
33 
34 #include <netlink-private/netlink.h>
35 #include <netlink-private/tc.h>
36 #include <netlink/netlink.h>
37 #include <netlink/utils.h>
38 #include <netlink/route/pktloc.h>
39 
40 #include "pktloc_syntax.h"
41 #include "pktloc_grammar.h"
42 
43 /** @cond SKIP */
44 #define PKTLOC_NAME_HT_SIZ 256
45 
46 static struct nl_list_head pktloc_name_ht[PKTLOC_NAME_HT_SIZ];
47 
48 /* djb2 */
49 static unsigned int pktloc_hash(const char *str)
50 {
51  unsigned long hash = 5381;
52  int c;
53 
54  while ((c = *str++))
55  hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
56 
57  return hash % PKTLOC_NAME_HT_SIZ;
58 }
59 
60 static int __pktloc_lookup(const char *name, struct rtnl_pktloc **result)
61 {
62  struct rtnl_pktloc *loc;
63  int hash;
64 
65  hash = pktloc_hash(name);
66  nl_list_for_each_entry(loc, &pktloc_name_ht[hash], list) {
67  if (!strcasecmp(loc->name, name)) {
68  loc->refcnt++;
69  *result = loc;
70  return 0;
71  }
72  }
73 
74  return -NLE_OBJ_NOTFOUND;
75 }
76 
77 extern int pktloc_parse(void *scanner);
78 
79 static void rtnl_pktloc_free(struct rtnl_pktloc *loc)
80 {
81  if (!loc)
82  return;
83 
84  free(loc->name);
85  free(loc);
86 }
87 
88 static int read_pktlocs(void)
89 {
90  YY_BUFFER_STATE buf = NULL;
91  yyscan_t scanner = NULL;
92  static time_t last_read;
93  struct stat st;
94  char *path;
95  int i, err;
96  FILE *fd;
97 
98  if (build_sysconf_path(&path, "pktloc") < 0)
99  return -NLE_NOMEM;
100 
101  /* if stat fails, just try to read the file */
102  if (stat(path, &st) == 0) {
103  /* Don't re-read file if file is unchanged */
104  if (last_read == st.st_mtime)
105  return 0;
106  }
107 
108  NL_DBG(2, "Reading packet location file \"%s\"\n", path);
109 
110  if (!(fd = fopen(path, "r"))) {
111  err = -NLE_PKTLOC_FILE;
112  goto errout;
113  }
114 
115  for (i = 0; i < PKTLOC_NAME_HT_SIZ; i++) {
116  struct rtnl_pktloc *loc, *n;
117 
118  nl_list_for_each_entry_safe(loc, n, &pktloc_name_ht[i], list)
119  rtnl_pktloc_put(loc);
120 
121  nl_init_list_head(&pktloc_name_ht[i]);
122  }
123 
124  if ((err = pktloc_lex_init(&scanner)) < 0) {
125  err = -NLE_FAILURE;
126  goto errout_close;
127  }
128 
129  buf = pktloc__create_buffer(fd, YY_BUF_SIZE, scanner);
130  pktloc__switch_to_buffer(buf, scanner);
131 
132  if ((err = pktloc_parse(scanner)) != 0) {
133  pktloc__delete_buffer(buf, scanner);
134  err = -NLE_PARSE_ERR;
135  goto errout_scanner;
136  }
137 
138  last_read = st.st_mtime;
139 
140 errout_scanner:
141  if (scanner)
142  pktloc_lex_destroy(scanner);
143 errout_close:
144  fclose(fd);
145 errout:
146  free(path);
147 
148  return 0;
149 }
150 
151 /** @endcond */
152 
153 /**
154  * Lookup packet location alias
155  * @arg name Name of packet location.
156  * @arg result Result pointer
157  *
158  * Tries to find a matching packet location alias for the supplied
159  * packet location name.
160  *
161  * The file containing the packet location definitions is automatically
162  * re-read if its modification time has changed since the last call.
163  *
164  * The returned packet location has to be returned after use by calling
165  * rtnl_pktloc_put() in order to allow freeing its memory after the last
166  * user has abandoned it.
167  *
168  * @return 0 on success or a negative error code.
169  * @retval NLE_PKTLOC_FILE Unable to open packet location file.
170  * @retval NLE_OBJ_NOTFOUND No matching packet location alias found.
171  */
172 int rtnl_pktloc_lookup(const char *name, struct rtnl_pktloc **result)
173 {
174  int err;
175 
176  if ((err = read_pktlocs()) < 0)
177  return err;
178 
179  return __pktloc_lookup(name, result);
180 }
181 
182 /**
183  * Allocate packet location object
184  */
186 {
187  struct rtnl_pktloc *loc;
188 
189  if (!(loc = calloc(1, sizeof(*loc))))
190  return NULL;
191 
192  loc->refcnt = 1;
193  nl_init_list_head(&loc->list);
194 
195  return loc;
196 }
197 
198 /**
199  * Return reference of a packet location
200  * @arg loc packet location object.
201  */
202 void rtnl_pktloc_put(struct rtnl_pktloc *loc)
203 {
204  if (!loc)
205  return;
206 
207  loc->refcnt--;
208  if (loc->refcnt <= 0)
209  rtnl_pktloc_free(loc);
210 }
211 
212 /**
213  * Add a packet location to the hash table
214  * @arg loc packet location object
215  *
216  * @return 0 on success or a negative error code.
217  */
218 int rtnl_pktloc_add(struct rtnl_pktloc *loc)
219 {
220  struct rtnl_pktloc *l;
221 
222  if (__pktloc_lookup(loc->name, &l) == 0) {
223  rtnl_pktloc_put(l);
224  return -NLE_EXIST;
225  }
226 
227  NL_DBG(2, "New packet location entry \"%s\" align=%u layer=%u "
228  "offset=%u mask=%#x shift=%u refnt=%u\n",
229  loc->name, loc->align, loc->layer, loc->offset,
230  loc->mask, loc->shift, loc->refcnt);
231 
232  nl_list_add_tail(&loc->list, &pktloc_name_ht[pktloc_hash(loc->name)]);
233 
234  return 0;
235 }
236 
237 void rtnl_pktloc_foreach(void (*cb)(struct rtnl_pktloc *, void *), void *arg)
238 {
239  struct rtnl_pktloc *loc;
240  int i;
241 
242  /* ignore errors */
243  read_pktlocs();
244 
245  for (i = 0; i < PKTLOC_NAME_HT_SIZ; i++)
246  nl_list_for_each_entry(loc, &pktloc_name_ht[i], list)
247  cb(loc, arg);
248 }
249 
250 static int __init pktloc_init(void)
251 {
252  int i;
253 
254  for (i = 0; i < PKTLOC_NAME_HT_SIZ; i++)
255  nl_init_list_head(&pktloc_name_ht[i]);
256 
257  return 0;
258 }
259 
260 /** @} */