metrics.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <linux/netlink.h>
  2. #include <linux/rtnetlink.h>
  3. #include <linux/types.h>
  4. #include <net/ip.h>
  5. #include <net/net_namespace.h>
  6. #include <net/tcp.h>
  7. int ip_metrics_convert(struct net *net, struct nlattr *fc_mx, int fc_mx_len,
  8. u32 *metrics)
  9. {
  10. bool ecn_ca = false;
  11. struct nlattr *nla;
  12. int remaining;
  13. if (!fc_mx)
  14. return 0;
  15. nla_for_each_attr(nla, fc_mx, fc_mx_len, remaining) {
  16. int type = nla_type(nla);
  17. u32 val;
  18. if (!type)
  19. continue;
  20. if (type > RTAX_MAX)
  21. return -EINVAL;
  22. if (type == RTAX_CC_ALGO) {
  23. char tmp[TCP_CA_NAME_MAX];
  24. nla_strlcpy(tmp, nla, sizeof(tmp));
  25. val = tcp_ca_get_key_by_name(net, tmp, &ecn_ca);
  26. if (val == TCP_CA_UNSPEC)
  27. return -EINVAL;
  28. } else {
  29. if (nla_len(nla) != sizeof(u32))
  30. return -EINVAL;
  31. val = nla_get_u32(nla);
  32. }
  33. if (type == RTAX_ADVMSS && val > 65535 - 40)
  34. val = 65535 - 40;
  35. if (type == RTAX_MTU && val > 65535 - 15)
  36. val = 65535 - 15;
  37. if (type == RTAX_HOPLIMIT && val > 255)
  38. val = 255;
  39. if (type == RTAX_FEATURES && (val & ~RTAX_FEATURE_MASK))
  40. return -EINVAL;
  41. metrics[type - 1] = val;
  42. }
  43. if (ecn_ca)
  44. metrics[RTAX_FEATURES - 1] |= DST_FEATURE_ECN_CA;
  45. return 0;
  46. }
  47. EXPORT_SYMBOL_GPL(ip_metrics_convert);