mod_ip6_qos.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * mod_ip6_qos.c
  3. *
  4. * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
  5. *
  6. */
  7. #include "config.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "argv.h"
  12. #include "mod.h"
  13. #include "pkt.h"
  14. struct ip6_qos_data
  15. {
  16. int ip6_tc;
  17. int ip6_fl;
  18. };
  19. static void *
  20. ip6_qos_close(void *d)
  21. {
  22. if (d != NULL)
  23. free(d);
  24. return (NULL);
  25. }
  26. static void *
  27. ip6_qos_open(int argc, char *argv[])
  28. {
  29. struct ip6_qos_data *data;
  30. if (argc != 3) {
  31. return NULL;
  32. }
  33. if ((data = calloc(1, sizeof(*data))) == NULL)
  34. return (NULL);
  35. if (sscanf(argv[1], "%x", (unsigned int*)&data->ip6_tc) != 1 ||
  36. data->ip6_tc < 0 || data->ip6_tc > 255)
  37. return (ip6_qos_close(data));
  38. if (sscanf(argv[2], "%x", (unsigned int*)&data->ip6_fl) != 1 ||
  39. data->ip6_fl < 0 || data->ip6_fl > 0x100000)
  40. return (ip6_qos_close(data));
  41. printf("init: %x\n", data->ip6_fl);
  42. return (data);
  43. }
  44. static int
  45. ip6_qos_apply(void *d, struct pktq *pktq)
  46. {
  47. struct ip6_qos_data *data = (struct ip6_qos_data *)d;
  48. struct pkt *pkt;
  49. TAILQ_FOREACH(pkt, pktq, pkt_next) {
  50. uint16_t eth_type = htons(pkt->pkt_eth->eth_type);
  51. if (eth_type == ETH_TYPE_IPV6) {
  52. if (data->ip6_tc || data->ip6_fl) {
  53. pkt->pkt_ip6->ip6_flow = htonl((uint32_t)data->ip6_tc << 20 |
  54. data->ip6_fl);
  55. pkt->pkt_ip6->ip6_vfc = (IP6_VERSION | (data->ip6_tc >> 4));
  56. }
  57. }
  58. }
  59. return (0);
  60. }
  61. struct mod mod_ip6_qos = {
  62. "ip6_qos", /* name */
  63. "ip6_qos <tc> <fl>", /* usage */
  64. ip6_qos_open, /* open */
  65. ip6_qos_apply, /* apply */
  66. ip6_qos_close /* close */
  67. };