]> The Tcpdump Group git mirrors - libpcap/blob - doc/README.capture-module
Clean up some whitespaces
[libpcap] / doc / README.capture-module
1 How to write a libpcap module
2
3 WARNING: this document describes an unstable interface; future releases
4 of libpcap may, and some probably will, change the interface in an
5 incompatible fashion. If you submit your module to the libpcap
6 developers for inclusion in libpcap, not only does that make it more
7 likely that it will be available in the libpcap provided by operating
8 system vendors (such as Linux distributions), but it also means that we
9 will attempt to update it to handle future changes to this interface.
10 If we add new capabilities, we may have to ask you how to provide those
11 additional capabilities if you're using an underlying mechanism for
12 which we have neither the source code nor the documentation.
13
14 NOTE: this document assumes familiarity with the entire libpcap API.
15
16 TODO: more routines, more stuff that the activate routine has to do
17 (such as setting the list of DLT_s), convert to Markdown?
18
19 On Linux, *BSD, macOS, Solaris, AIX, HP-UX, IRIX, and Tru64 UNIX,
20 Libpcap supports capturing on network interfaces as supported by the
21 operating system networking stack, using the native packet capture
22 mechanism provided by the OS. On Windows, it supports it with the help
23 of the driver and library supplied by WinPcap and Npcap.
24
25 In addition, it also supports capturing on other types of devices, such
26 as:
27
28 specialized capture cards, such as Endace DAG cards;
29
30 network adapters that provide special high-performance code
31 paths, such as CSPI Myricom adapters;
32
33 buses such as USB;
34
35 software communication channels such as D-Bus and Linux netlink;
36
37 etc..
38
39 Support for those devices is provided by modules compiled into libpcap.
40
41 If you want to add such a module, you would first have to check the list
42 of link-layer header types supported by libpcap, to see if one of those
43 would be sufficient for your device. The current version of the list
44 can be found at
45
46 https://www.tcpdump.org/linktypes.html
47
48 If none of those would work for your device, please read
49 doc/DLT_ALLOCATE_HOWTO.md and the introductory paragraphs on the Web
50 page mentioned above, and then send a request for the new link-layer
51 header type to tcpdump-workers@lists.tcpdump.org.
52
53 Once you have a link-layer header type value or values that you can use,
54 you can add new module.
55
56 The module should be a C source file, with a name of the form
57 pcap-{MOD}.c, where {MOD} is a name appropriate for your device; for
58 example, the support for DAG cards is in pcap-dag.c, and the support for
59 capturing USB traffic on Linux is pcap-usb-linux.c.
60
61 Your module is assumed to support one or more named devices. The names
62 should be relatively short names, containing only lower-case
63 alphanumeric characters, consisting of a prefix that ends with an
64 alphabetic character and, if there can be more than one device instance,
65 possibly followed by a numerical device ID, such as "mydevice" or
66 "mydevice0"/"mydevice1"/.... If you have more than one type of device
67 that you can support, you can have more than one prefix, each of which
68 can be followed by a numerical device ID.
69
70 The two exported functions that your module must provide are routines to
71 provide a list of device instances and a program to initialize a
72 created-but-not-activated pcap_t for an instance of one of your devices.
73
74 The "list of device instances" routine takes, as arguments:
75
76 a pointer to a pcap_if_list_t;
77
78 a pointer to an error message buffer.
79
80 The error message buffer may be assumed to be PCAP_ERRBUF_SIZE bytes
81 large, but must not be assumed to be larger. By convention, the routine
82 typically has a name containing "findalldevs".
83
84 The routine should attempt to determine what device instances are
85 available and add them to the list pointed to by the first argument;
86 this may be impossible for some modules, but, for those modules, it may
87 be difficult to capture on the devices using Wirehshark (although it
88 should be possible to capture on them using tcpdump, TShark, or other
89 programs that take a device name on the command line), so we recommend
90 that your routine provide the list of devices if possible. If it
91 cannot, it should just immediately return 0.
92
93 The routine should add devices to the list by calling the add_dev()
94 routine in libpcap, declared in the pcap-int.h header. It takes, as
95 arguments:
96
97 the pointer to the pcap_if_list_t passed as an argument to the
98 routine;
99
100 the device name, as described above;
101
102 a 32-bit word of flags, as provided by pcap_findalldevs();
103
104 a text description of the device, or NULL if there is no
105 description;
106
107 the error message buffer pointer provided to the routine.
108
109 add_dev() will, if it succeeds, return a pointer to a pcap_if_t that was
110 added to the list of devices. If it fails, it will return NULL; in this
111 case, the error message buffer has been filled in with an error string,
112 and your routine must return -1 to indicate the error.
113
114 If your routine succeeds, it must return 0. If it fails, it must fill
115 in the error message buffer with an error string and return -1.
116
117 The "initialize the pcap_t" routine takes, as arguments:
118
119 a pointer to a device name;
120
121 a pointer to an error message buffer;
122
123 a pointer to an int.
124
125 It returns a pointer to a pcap_t.
126
127 Your module will probably need, for each pcap_t for an opened device, a
128 private data structure to maintain its own information about the opened
129 device. These should be allocated per opened instance, not per device;
130 if, for example, mydevice0 can be captured on by more than one program
131 at the same time, there will be more than one pcap_t opened for
132 mydevice0, and so there will be separate private data structures for
133 each pcap_t. If you need to maintain per-device, rather than per-opened
134 instance information, you will have to maintain that yourself.
135
136 The routine should first check the device to see whether it looks like a
137 device that this module would handle; for example, it should begin with
138 one of the device name prefixes for your module and, if your devices
139 have instance numbers, be followed by a number. If it is not one of
140 those devices, you must set the integer pointed to by the third
141 argument to 0, to indicate that this is *not* one of the devices for
142 your module, and return NULL.
143
144 If it *is* one of those devices, it should call pcap_create_common,
145 passing to it the error message buffer as the first argument and the
146 size of the per-opened instance data structure as the second argument.
147 If it fails, it will return NULL; you must return NULL in this case.
148
149 If it succeeds, the pcap_t pointed to by the return value has been
150 partially initialized, but you will need to complete the process. It
151 has a "priv" member, which is a void * that points to the private data
152 structure attached to it; that structure has been initialized to zeroes.
153
154 What you need to set are some function pointers to your routines to
155 handle certain operations:
156
157 activate_op
158 the routine called when pcap_activate() is done on the
159 pcap_t
160
161 can_set_rfmon_op
162 the routine called when pcap_can_set_rfmon() is done on
163 the pcap_t - if your device doesn't support 802.11
164 monitor mode, you can leave this as initialized by
165 pcap_create_common(), as that routine will return "no,
166 monitor mode isn't supported".
167
168 Once you've set the activate_op and, if necessary, the can_set_rfmon_op,
169 you must return the pcap_t * that was returned to you.
170
171 Your activate routine takes, as an argument, a pointer to the pcap_t
172 being activated, and returns an int.
173
174 The perameters set for the device in the pcap_create() call, and after
175 that call(), are mostly in the opt member of the pcap_t:
176
177 device
178 the name of the device
179
180 timeout
181 the buffering timeout, in milliseconds
182
183 buffer_size
184 the buffer size to use
185
186 promisc
187 1 if promiscuous mode is to be used, 0 otherwise
188
189 rfmon
190 1 if monitor mode is to be used, 0 otherwise
191
192 immediate
193 1 if the device should be in immediate mode, 0 otherwise
194
195 nonblock
196 1 if the device should be in non-blocking mode, 0
197 otherwise
198
199 tstamp_type
200 the type of time stamp to supply
201
202 tstamp_precision
203 the time stamp precision to supply
204
205 The snapshot member of the pcap_t structure will contain the snapshot
206 length to be used.
207
208 Your routine should attempt to set up the device for capturing. If it
209 fails, it must return an error indication which is one of the PCAP_ERROR
210 values. For PCAP_ERROR, it must also set the errbuf member of the
211 pcap_t to an error string. For PCAP_ERROR_NO_SUCH_DEVICE and
212 PCAP_ERROR_PERM_DENIED, it may set it to an error string providing
213 additional information that may be useful for debugging, or may just
214 leave it as a null string.
215
216 If it succeeds, it must set certain function pointers in the pcap_t
217 structure:
218
219 read_op
220 called whenever packets are to be read
221
222 inject_op
223 called whenever packets are to be injected
224
225 setfilter_op
226 called whenever pcap_setfilter() is called
227
228 setdirection_op
229 called whenever pcap_setdirection() is called
230
231 set_datalink_op
232 called whnever pcap_set_datalink() is called
233
234 getnonblock_op
235 called whenver pcap_getnonblock() is called
236
237 setnonblock_op
238 called whenever pcap_setnonblock() is called
239
240 stats_op
241 called whenver pcap_stats() is called
242
243 cleanup_op
244 called if the activate routine fails or pcap_close() is
245 called
246
247 and must also set the linktype member to the DLT_ value for the device.
248
249 On UN*Xes, if the device supports waiting for packets to arrive with
250 select()/poll()/epoll()/kqueues etc., it should set the selectable_fd
251 member of the structure to the descriptor you would use with those
252 calls. If it does not, then, if that's because the device polls for
253 packets rather than receiving interrupts or other signals when packets
254 arrive, it should have a struct timeval in the private data structure,
255 set the value of that struct timeval to the poll timeout, and set the
256 required_select_timeout member of the pcap_t to point to the struct
257 timeval.
258
259 The read_op routine is called when pcap_dispatch(), pcap_loop(),
260 pcap_next(), or pcap_next_ex() is called. It is passed the same
261 arguments as pcap_dispatch() is called.
262
263 The routine should first check if the break_loop member of the pcap_t is
264 non-zero and, if so, set that member to zero and return
265 PCAP_ERROR_BREAK.
266
267 Then, if the pcap_t is in blocking mode (as opposed to non-blocking
268 mode), and there are no packets immediately available to be passed to
269 the callback, it should block waiting for packets to arrive, using the
270 buffering timeout, first, and read packets from the device if necessary.
271
272 Then it should loop through the available packets, calling the callback
273 routine for each packet:
274
275 If the PACKET_COUNT_IS_UNLIMITED() macro evaluates to true when
276 passed the packet count argument, the loop should continue until
277 there are no more packets immediately available or the
278 break_loop member of the pcap_t is non-zero. If the break_loop
279 member is fount to be non-zero, it should set that member to
280 zero and return PCAP_ERROR_BREAK.
281
282 If it doesn't evaluat to true, then the loop should also
283 terminate if the specified number of packets have been delivered
284 to the callback.
285
286 Note that there is *NO* requirement that the packet header or data
287 provided to the callback remain available, or valid, after the callback
288 routine returns; if the callback needs to save the data for other code
289 to use, it must make a copy of that data. This means that the module is
290 free to, for example, overwrite the buffer into which it read the
291 packet, or release back to the kernel a packet in a memory-mapped
292 buffer shared between the kernel and userland, after the callback
293 returns.
294
295 If an error occurs when reading packets from the device, it must set the
296 errbuf member of the pcap_t to an error string and return PCAP_ERROR.
297
298 If no error occurs, it must return the number of packets that were
299 supplied to the callback routine.
300
301 The inject routine is passed a pointer to the pcap_t, a buffer
302 containing the contents of the packet to inject, and the number of bytes
303 in the packet. If the device doesn't support packet injection, the
304 routine must set the errbuf member of the pcap_t to a message indicating
305 that packet injection isn't supported and return PCAP_ERROR. Otherwise,
306 it should attempt to inject the packet; if the attempt fails, it must
307 set the errbuf member of the pcap_t to an error message and return
308 PCAP_ERROR. Otherwise, it should return the number of bytes injected.
309
310 The setfilter routine is passed a pointer to the pcap_t and a pointer
311 to a struct bpf_program containing a BPF program to be used as a filter.
312 If the mechanism used by your module can perform filtering with a BPF
313 program, it would attempt to set that filter to the specified program.
314
315 If that failed because the program was too large, or used BPF features
316 not supported by that mechanism, the module should fall back on
317 filtering in userland by saving a copy of the filter with a call to
318 install_bpf_program(), setting a flag in the private data instructure
319 indicating that filtering is being done by the module and, in the read
320 routine's main loop, checking the flag and, if it's set, calling
321 pcap_filter(), passing it the fcode.bf_insns member of the pcap_t, the
322 raw packet data, the on-the-wire length of the packet, and the captured
323 length of the packet, and only passing the packet to the callback
324 routine, and counting it, if pcap_filter() returns a non-zero value.
325 (If the flag is not set, all packets should be passed to the callback
326 routine and counted, as the filtering is being done by the mechanism
327 used by the module.) If install_bpf_program() returns a negative value,
328 the routine should return PCAP_ERROR.
329
330 If the attempt to set the filter failed for any other reason, the
331 routine must set the errbuf member of the pcap_t to an error message and
332 return PCAP_ERROR.
333
334 If the attempt to set the filter succeeded, or it failed because the
335 mechanism used by the module rejected it and the call to
336 install_bpf_program() succeeded, the routine should return 0.
337
338 If the mechanism the module uses doesn't support filtering, the pointer
339 to the setfilter routine can just be set to point to
340 install_bpf_program; the module does not need a routine of its own to
341 handle that.
342
343 The setdirection routine is passed a pointer to the pcap_t and a
344 pcap_direction_t indicating which packet directions should be accepted.
345 If the module can't arrange to handle only incoming packets or only
346 outgoing packets, it can set the pointer to the setdirection routine to
347 NULL, and calls to pcap_setdirection() will fail with an error message
348 indicating that setting the direction isn't supported.
349
350 XXX describe set_datalink, including what the activate routine has to do
351 XXX
352
353 XXX describe the rest of the routines XXX