act_vlan.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * Copyright (c) 2014 Jiri Pirko <jiri@resnulli.us>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/rtnetlink.h>
  14. #include <linux/if_vlan.h>
  15. #include <net/netlink.h>
  16. #include <net/pkt_sched.h>
  17. #include <linux/tc_act/tc_vlan.h>
  18. #include <net/tc_act/tc_vlan.h>
  19. #define VLAN_TAB_MASK 15
  20. static int vlan_net_id;
  21. static struct tc_action_ops act_vlan_ops;
  22. static int tcf_vlan(struct sk_buff *skb, const struct tc_action *a,
  23. struct tcf_result *res)
  24. {
  25. struct tcf_vlan *v = to_vlan(a);
  26. int action;
  27. int err;
  28. spin_lock(&v->tcf_lock);
  29. tcf_lastuse_update(&v->tcf_tm);
  30. bstats_update(&v->tcf_bstats, skb);
  31. action = v->tcf_action;
  32. switch (v->tcfv_action) {
  33. case TCA_VLAN_ACT_POP:
  34. err = skb_vlan_pop(skb);
  35. if (err)
  36. goto drop;
  37. break;
  38. case TCA_VLAN_ACT_PUSH:
  39. err = skb_vlan_push(skb, v->tcfv_push_proto, v->tcfv_push_vid);
  40. if (err)
  41. goto drop;
  42. break;
  43. default:
  44. BUG();
  45. }
  46. goto unlock;
  47. drop:
  48. action = TC_ACT_SHOT;
  49. v->tcf_qstats.drops++;
  50. unlock:
  51. spin_unlock(&v->tcf_lock);
  52. return action;
  53. }
  54. static const struct nla_policy vlan_policy[TCA_VLAN_MAX + 1] = {
  55. [TCA_VLAN_PARMS] = { .len = sizeof(struct tc_vlan) },
  56. [TCA_VLAN_PUSH_VLAN_ID] = { .type = NLA_U16 },
  57. [TCA_VLAN_PUSH_VLAN_PROTOCOL] = { .type = NLA_U16 },
  58. };
  59. static int tcf_vlan_init(struct net *net, struct nlattr *nla,
  60. struct nlattr *est, struct tc_action **a,
  61. int ovr, int bind)
  62. {
  63. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  64. struct nlattr *tb[TCA_VLAN_MAX + 1];
  65. struct tc_vlan *parm;
  66. struct tcf_vlan *v;
  67. int action;
  68. __be16 push_vid = 0;
  69. __be16 push_proto = 0;
  70. bool exists = false;
  71. int ret = 0, err;
  72. if (!nla)
  73. return -EINVAL;
  74. err = nla_parse_nested(tb, TCA_VLAN_MAX, nla, vlan_policy);
  75. if (err < 0)
  76. return err;
  77. if (!tb[TCA_VLAN_PARMS])
  78. return -EINVAL;
  79. parm = nla_data(tb[TCA_VLAN_PARMS]);
  80. exists = tcf_hash_check(tn, parm->index, a, bind);
  81. if (exists && bind)
  82. return 0;
  83. switch (parm->v_action) {
  84. case TCA_VLAN_ACT_POP:
  85. break;
  86. case TCA_VLAN_ACT_PUSH:
  87. if (!tb[TCA_VLAN_PUSH_VLAN_ID]) {
  88. if (exists)
  89. tcf_hash_release(*a, bind);
  90. return -EINVAL;
  91. }
  92. push_vid = nla_get_u16(tb[TCA_VLAN_PUSH_VLAN_ID]);
  93. if (push_vid >= VLAN_VID_MASK) {
  94. if (exists)
  95. tcf_hash_release(*a, bind);
  96. return -ERANGE;
  97. }
  98. if (tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]) {
  99. push_proto = nla_get_be16(tb[TCA_VLAN_PUSH_VLAN_PROTOCOL]);
  100. switch (push_proto) {
  101. case htons(ETH_P_8021Q):
  102. case htons(ETH_P_8021AD):
  103. break;
  104. default:
  105. return -EPROTONOSUPPORT;
  106. }
  107. } else {
  108. push_proto = htons(ETH_P_8021Q);
  109. }
  110. break;
  111. default:
  112. if (exists)
  113. tcf_hash_release(*a, bind);
  114. return -EINVAL;
  115. }
  116. action = parm->v_action;
  117. if (!exists) {
  118. ret = tcf_hash_create(tn, parm->index, est, a,
  119. &act_vlan_ops, bind, false);
  120. if (ret)
  121. return ret;
  122. ret = ACT_P_CREATED;
  123. } else {
  124. tcf_hash_release(*a, bind);
  125. if (!ovr)
  126. return -EEXIST;
  127. }
  128. v = to_vlan(*a);
  129. spin_lock_bh(&v->tcf_lock);
  130. v->tcfv_action = action;
  131. v->tcfv_push_vid = push_vid;
  132. v->tcfv_push_proto = push_proto;
  133. v->tcf_action = parm->action;
  134. spin_unlock_bh(&v->tcf_lock);
  135. if (ret == ACT_P_CREATED)
  136. tcf_hash_insert(tn, *a);
  137. return ret;
  138. }
  139. static int tcf_vlan_dump(struct sk_buff *skb, struct tc_action *a,
  140. int bind, int ref)
  141. {
  142. unsigned char *b = skb_tail_pointer(skb);
  143. struct tcf_vlan *v = to_vlan(a);
  144. struct tc_vlan opt = {
  145. .index = v->tcf_index,
  146. .refcnt = v->tcf_refcnt - ref,
  147. .bindcnt = v->tcf_bindcnt - bind,
  148. .action = v->tcf_action,
  149. .v_action = v->tcfv_action,
  150. };
  151. struct tcf_t t;
  152. if (nla_put(skb, TCA_VLAN_PARMS, sizeof(opt), &opt))
  153. goto nla_put_failure;
  154. if (v->tcfv_action == TCA_VLAN_ACT_PUSH &&
  155. (nla_put_u16(skb, TCA_VLAN_PUSH_VLAN_ID, v->tcfv_push_vid) ||
  156. nla_put_be16(skb, TCA_VLAN_PUSH_VLAN_PROTOCOL,
  157. v->tcfv_push_proto)))
  158. goto nla_put_failure;
  159. tcf_tm_dump(&t, &v->tcf_tm);
  160. if (nla_put_64bit(skb, TCA_VLAN_TM, sizeof(t), &t, TCA_VLAN_PAD))
  161. goto nla_put_failure;
  162. return skb->len;
  163. nla_put_failure:
  164. nlmsg_trim(skb, b);
  165. return -1;
  166. }
  167. static int tcf_vlan_walker(struct net *net, struct sk_buff *skb,
  168. struct netlink_callback *cb, int type,
  169. const struct tc_action_ops *ops)
  170. {
  171. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  172. return tcf_generic_walker(tn, skb, cb, type, ops);
  173. }
  174. static int tcf_vlan_search(struct net *net, struct tc_action **a, u32 index)
  175. {
  176. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  177. return tcf_hash_search(tn, a, index);
  178. }
  179. static struct tc_action_ops act_vlan_ops = {
  180. .kind = "vlan",
  181. .type = TCA_ACT_VLAN,
  182. .owner = THIS_MODULE,
  183. .act = tcf_vlan,
  184. .dump = tcf_vlan_dump,
  185. .init = tcf_vlan_init,
  186. .walk = tcf_vlan_walker,
  187. .lookup = tcf_vlan_search,
  188. .size = sizeof(struct tcf_vlan),
  189. };
  190. static __net_init int vlan_init_net(struct net *net)
  191. {
  192. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  193. return tc_action_net_init(tn, &act_vlan_ops, VLAN_TAB_MASK);
  194. }
  195. static void __net_exit vlan_exit_net(struct net *net)
  196. {
  197. struct tc_action_net *tn = net_generic(net, vlan_net_id);
  198. tc_action_net_exit(tn);
  199. }
  200. static struct pernet_operations vlan_net_ops = {
  201. .init = vlan_init_net,
  202. .exit = vlan_exit_net,
  203. .id = &vlan_net_id,
  204. .size = sizeof(struct tc_action_net),
  205. };
  206. static int __init vlan_init_module(void)
  207. {
  208. return tcf_register_action(&act_vlan_ops, &vlan_net_ops);
  209. }
  210. static void __exit vlan_cleanup_module(void)
  211. {
  212. tcf_unregister_action(&act_vlan_ops, &vlan_net_ops);
  213. }
  214. module_init(vlan_init_module);
  215. module_exit(vlan_cleanup_module);
  216. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  217. MODULE_DESCRIPTION("vlan manipulation actions");
  218. MODULE_LICENSE("GPL v2");