120 likes | 250 Views
The Anatomy of a Plugin. Plugins. Every plugin MUST reply to 4 messages (defined in ann/usr/src/sys/crossbow/cb_var.h): create instance free instance register instance deregister instance Plugin CAN reply to set data with filter get data from filter free data from filter. Typical Plugin.
E N D
Plugins • Every plugin MUST reply to 4 messages (defined in ann/usr/src/sys/crossbow/cb_var.h): • create instance • free instance • register instance • deregister instance • Plugin CAN reply to • set data with filter • get data from filter • free data from filter
Typical Plugin • A plugin is C code that is dynamically linked with the kernel • Need some special functions, so we use a template • Typically Consists of (at least) three files • cbpgi.c • [myplugin].h • [myplugin].c
cbpgi.c • Implements: • basic interaction with kernel • mandatory message dispatching • determines instance number • interface to plugin specific code by calling my_handle_packet() • Typically does not have to be changed.
myplugin.c • plugin specific functions • implements my_handle_packet() • called by cbpgi.c code for each packet given to the plugin • Arguments: • mbuf for the current packet • instance number • plugin code can then keep instance specific state • Suggested usage: • write functions to perform your operations • call your functions from my_handle_packet() • build up a set of useful functions to re-use
myplugin.h • Plugin Specific #defines • e.g.: #define CBPGICODE 88 /* Crossbow plugin id code */ #define CBPGINAME "myplugin" /* LKM name */ #define CBPGIINUM 4 /* max number of instances */ • Change these to change the definition of your plugin: • Need a unique plugin id code • Set the name of your plugin • Control the number of instances your plugin is allowed to create
Plugin Name • e.g: To change the name of your plugin to foobar • mv myplugin.c foobar.c • mv myplugin.h foobar.h [Edit the files: Makefile, cbpgi.c, foobar.c, foobar.h and change all occurrences of myplugin to foobar]
Utility Functions • We’ve developed some mbuf and IP utility functions • some of which you will use in the exercises • mbuf • printMbufHdrFct() • IP • locateIpHdrFct() • printIpHdrFct() • UDP • locateUdpHdrFct() • printUdpHdrFct() • locateUdpPayloadFct() • pu_udpcksum() • TCP • similar to UDP • Payload • printPayloadFct()
Example: PrintPacket (we’ll use this in the Exercises) // DISPATCH and MOD_MISC defined in /usr/include/sys/lkm.h // MOD_MISC defines lkm struct for this plugin MOD_MISC(CBPGINAME) /* External entry point for modload and modunload */ int PrintPacket ( struct lkm_table *lkmtp, int cmd, int ver) { DISPATCH(lkmtp,cmd,ver,cbpgi_handle,cbpgi_handle,lkm_nofunc) } /* Call the functions to perform the operations you desire. */ void my_handle_packet (int inum, struct mbuf **packet) { PrintPacketFct(inum, packet); return; } (continued…)
Example: PrintPacket (we’ll use this in the Exercises) void PrintPacketFct (int inum, struct mbuf **packet) { struct ip *iph=0; struct udphdr *udph=0; struct tcphdr *tcph=0; unsigned char *payload=0; int payloadSize = 0; printMbufHdrFct(inum, packet); iph = locateIpHdrFct(inum, packet); if (iph) { (continued…)
Example: PrintPacket (continued) printIpHdrFct(inum, iph); switch(iph->ip_p) { case IPPROTO_UDP: udph = locateUdpHdrFct(inum, iph); if (udph) { printUdpHdrFct(inum, udph); payload = locateUdpPayloadFct(inum, iph, &payloadSize); } break; case IPPROTO_TCP: tcph = locateTcpHdrFct(inum, iph); if (tcph) { printTcpHdrFct(inum, tcph); payload = locateTcpPayloadFct(inum, iph, &payloadSize); } break; } (continued…)
Example: PrintPacket (continued) if (payload) { printPayloadFct(inum, payload, payloadSize); } } return; }