1 / 12

The Anatomy of a Plugin

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.

orien
Download Presentation

The Anatomy of a Plugin

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Anatomy of a Plugin

  2. 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

  3. 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

  4. 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.

  5. 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

  6. 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

  7. 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]

  8. 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()

  9. 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…)

  10. 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…)

  11. 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…)

  12. Example: PrintPacket (continued) if (payload) { printPayloadFct(inum, payload, payloadSize); } } return; }

More Related