xt_l2tp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /* Kernel module to match L2TP header parameters. */
  2. /* (C) 2013 James Chapman <jchapman@katalix.com>
  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 version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/module.h>
  10. #include <linux/skbuff.h>
  11. #include <linux/if_ether.h>
  12. #include <net/ip.h>
  13. #include <linux/ipv6.h>
  14. #include <net/ipv6.h>
  15. #include <net/udp.h>
  16. #include <linux/l2tp.h>
  17. #include <linux/netfilter_ipv4.h>
  18. #include <linux/netfilter_ipv6.h>
  19. #include <linux/netfilter_ipv4/ip_tables.h>
  20. #include <linux/netfilter_ipv6/ip6_tables.h>
  21. #include <linux/netfilter/x_tables.h>
  22. #include <linux/netfilter/xt_tcpudp.h>
  23. #include <linux/netfilter/xt_l2tp.h>
  24. /* L2TP header masks */
  25. #define L2TP_HDR_T_BIT 0x8000
  26. #define L2TP_HDR_L_BIT 0x4000
  27. #define L2TP_HDR_VER 0x000f
  28. MODULE_LICENSE("GPL");
  29. MODULE_AUTHOR("James Chapman <jchapman@katalix.com>");
  30. MODULE_DESCRIPTION("Xtables: L2TP header match");
  31. MODULE_ALIAS("ipt_l2tp");
  32. MODULE_ALIAS("ip6t_l2tp");
  33. /* The L2TP fields that can be matched */
  34. struct l2tp_data {
  35. u32 tid;
  36. u32 sid;
  37. u8 type;
  38. u8 version;
  39. };
  40. union l2tp_val {
  41. __be16 val16[2];
  42. __be32 val32;
  43. };
  44. static bool l2tp_match(const struct xt_l2tp_info *info, struct l2tp_data *data)
  45. {
  46. if ((info->flags & XT_L2TP_TYPE) && (info->type != data->type))
  47. return false;
  48. if ((info->flags & XT_L2TP_VERSION) && (info->version != data->version))
  49. return false;
  50. /* Check tid only for L2TPv3 control or any L2TPv2 packets */
  51. if ((info->flags & XT_L2TP_TID) &&
  52. ((data->type == XT_L2TP_TYPE_CONTROL) || (data->version == 2)) &&
  53. (info->tid != data->tid))
  54. return false;
  55. /* Check sid only for L2TP data packets */
  56. if ((info->flags & XT_L2TP_SID) && (data->type == XT_L2TP_TYPE_DATA) &&
  57. (info->sid != data->sid))
  58. return false;
  59. return true;
  60. }
  61. /* Parse L2TP header fields when UDP encapsulation is used. Handles
  62. * L2TPv2 and L2TPv3. Note the L2TPv3 control and data packets have a
  63. * different format. See
  64. * RFC2661, Section 3.1, L2TPv2 Header Format
  65. * RFC3931, Section 3.2.1, L2TPv3 Control Message Header
  66. * RFC3931, Section 3.2.2, L2TPv3 Data Message Header
  67. * RFC3931, Section 4.1.2.1, L2TPv3 Session Header over UDP
  68. */
  69. static bool l2tp_udp_mt(const struct sk_buff *skb, struct xt_action_param *par, u16 thoff)
  70. {
  71. const struct xt_l2tp_info *info = par->matchinfo;
  72. int uhlen = sizeof(struct udphdr);
  73. int offs = thoff + uhlen;
  74. union l2tp_val *lh;
  75. union l2tp_val lhbuf;
  76. u16 flags;
  77. struct l2tp_data data = { 0, };
  78. if (par->fragoff != 0)
  79. return false;
  80. /* Extract L2TP header fields. The flags in the first 16 bits
  81. * tell us where the other fields are.
  82. */
  83. lh = skb_header_pointer(skb, offs, 2, &lhbuf);
  84. if (lh == NULL)
  85. return false;
  86. flags = ntohs(lh->val16[0]);
  87. if (flags & L2TP_HDR_T_BIT)
  88. data.type = XT_L2TP_TYPE_CONTROL;
  89. else
  90. data.type = XT_L2TP_TYPE_DATA;
  91. data.version = (u8) flags & L2TP_HDR_VER;
  92. /* Now extract the L2TP tid/sid. These are in different places
  93. * for L2TPv2 (rfc2661) and L2TPv3 (rfc3931). For L2TPv2, we
  94. * must also check to see if the length field is present,
  95. * since this affects the offsets into the packet of the
  96. * tid/sid fields.
  97. */
  98. if (data.version == 3) {
  99. lh = skb_header_pointer(skb, offs + 4, 4, &lhbuf);
  100. if (lh == NULL)
  101. return false;
  102. if (data.type == XT_L2TP_TYPE_CONTROL)
  103. data.tid = ntohl(lh->val32);
  104. else
  105. data.sid = ntohl(lh->val32);
  106. } else if (data.version == 2) {
  107. if (flags & L2TP_HDR_L_BIT)
  108. offs += 2;
  109. lh = skb_header_pointer(skb, offs + 2, 4, &lhbuf);
  110. if (lh == NULL)
  111. return false;
  112. data.tid = (u32) ntohs(lh->val16[0]);
  113. data.sid = (u32) ntohs(lh->val16[1]);
  114. } else
  115. return false;
  116. return l2tp_match(info, &data);
  117. }
  118. /* Parse L2TP header fields for IP encapsulation (no UDP header).
  119. * L2TPv3 data packets have a different form with IP encap. See
  120. * RC3931, Section 4.1.1.1, L2TPv3 Session Header over IP.
  121. * RC3931, Section 4.1.1.2, L2TPv3 Control and Data Traffic over IP.
  122. */
  123. static bool l2tp_ip_mt(const struct sk_buff *skb, struct xt_action_param *par, u16 thoff)
  124. {
  125. const struct xt_l2tp_info *info = par->matchinfo;
  126. union l2tp_val *lh;
  127. union l2tp_val lhbuf;
  128. struct l2tp_data data = { 0, };
  129. /* For IP encap, the L2TP sid is the first 32-bits. */
  130. lh = skb_header_pointer(skb, thoff, sizeof(lhbuf), &lhbuf);
  131. if (lh == NULL)
  132. return false;
  133. if (lh->val32 == 0) {
  134. /* Must be a control packet. The L2TP tid is further
  135. * into the packet.
  136. */
  137. data.type = XT_L2TP_TYPE_CONTROL;
  138. lh = skb_header_pointer(skb, thoff + 8, sizeof(lhbuf),
  139. &lhbuf);
  140. if (lh == NULL)
  141. return false;
  142. data.tid = ntohl(lh->val32);
  143. } else {
  144. data.sid = ntohl(lh->val32);
  145. data.type = XT_L2TP_TYPE_DATA;
  146. }
  147. data.version = 3;
  148. return l2tp_match(info, &data);
  149. }
  150. static bool l2tp_mt4(const struct sk_buff *skb, struct xt_action_param *par)
  151. {
  152. struct iphdr *iph = ip_hdr(skb);
  153. u8 ipproto = iph->protocol;
  154. /* l2tp_mt_check4 already restricts the transport protocol */
  155. switch (ipproto) {
  156. case IPPROTO_UDP:
  157. return l2tp_udp_mt(skb, par, par->thoff);
  158. case IPPROTO_L2TP:
  159. return l2tp_ip_mt(skb, par, par->thoff);
  160. }
  161. return false;
  162. }
  163. #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
  164. static bool l2tp_mt6(const struct sk_buff *skb, struct xt_action_param *par)
  165. {
  166. unsigned int thoff = 0;
  167. unsigned short fragoff = 0;
  168. int ipproto;
  169. ipproto = ipv6_find_hdr(skb, &thoff, -1, &fragoff, NULL);
  170. if (fragoff != 0)
  171. return false;
  172. /* l2tp_mt_check6 already restricts the transport protocol */
  173. switch (ipproto) {
  174. case IPPROTO_UDP:
  175. return l2tp_udp_mt(skb, par, thoff);
  176. case IPPROTO_L2TP:
  177. return l2tp_ip_mt(skb, par, thoff);
  178. }
  179. return false;
  180. }
  181. #endif
  182. static int l2tp_mt_check(const struct xt_mtchk_param *par)
  183. {
  184. const struct xt_l2tp_info *info = par->matchinfo;
  185. /* Check for invalid flags */
  186. if (info->flags & ~(XT_L2TP_TID | XT_L2TP_SID | XT_L2TP_VERSION |
  187. XT_L2TP_TYPE)) {
  188. pr_info("unknown flags: %x\n", info->flags);
  189. return -EINVAL;
  190. }
  191. /* At least one of tid, sid or type=control must be specified */
  192. if ((!(info->flags & XT_L2TP_TID)) &&
  193. (!(info->flags & XT_L2TP_SID)) &&
  194. ((!(info->flags & XT_L2TP_TYPE)) ||
  195. (info->type != XT_L2TP_TYPE_CONTROL))) {
  196. pr_info("invalid flags combination: %x\n", info->flags);
  197. return -EINVAL;
  198. }
  199. /* If version 2 is specified, check that incompatible params
  200. * are not supplied
  201. */
  202. if (info->flags & XT_L2TP_VERSION) {
  203. if ((info->version < 2) || (info->version > 3)) {
  204. pr_info("wrong L2TP version: %u\n", info->version);
  205. return -EINVAL;
  206. }
  207. if (info->version == 2) {
  208. if ((info->flags & XT_L2TP_TID) &&
  209. (info->tid > 0xffff)) {
  210. pr_info("v2 tid > 0xffff: %u\n", info->tid);
  211. return -EINVAL;
  212. }
  213. if ((info->flags & XT_L2TP_SID) &&
  214. (info->sid > 0xffff)) {
  215. pr_info("v2 sid > 0xffff: %u\n", info->sid);
  216. return -EINVAL;
  217. }
  218. }
  219. }
  220. return 0;
  221. }
  222. static int l2tp_mt_check4(const struct xt_mtchk_param *par)
  223. {
  224. const struct xt_l2tp_info *info = par->matchinfo;
  225. const struct ipt_entry *e = par->entryinfo;
  226. const struct ipt_ip *ip = &e->ip;
  227. int ret;
  228. ret = l2tp_mt_check(par);
  229. if (ret != 0)
  230. return ret;
  231. if ((ip->proto != IPPROTO_UDP) &&
  232. (ip->proto != IPPROTO_L2TP)) {
  233. pr_info("missing protocol rule (udp|l2tpip)\n");
  234. return -EINVAL;
  235. }
  236. if ((ip->proto == IPPROTO_L2TP) &&
  237. (info->version == 2)) {
  238. pr_info("v2 doesn't support IP mode\n");
  239. return -EINVAL;
  240. }
  241. return 0;
  242. }
  243. #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
  244. static int l2tp_mt_check6(const struct xt_mtchk_param *par)
  245. {
  246. const struct xt_l2tp_info *info = par->matchinfo;
  247. const struct ip6t_entry *e = par->entryinfo;
  248. const struct ip6t_ip6 *ip = &e->ipv6;
  249. int ret;
  250. ret = l2tp_mt_check(par);
  251. if (ret != 0)
  252. return ret;
  253. if ((ip->proto != IPPROTO_UDP) &&
  254. (ip->proto != IPPROTO_L2TP)) {
  255. pr_info("missing protocol rule (udp|l2tpip)\n");
  256. return -EINVAL;
  257. }
  258. if ((ip->proto == IPPROTO_L2TP) &&
  259. (info->version == 2)) {
  260. pr_info("v2 doesn't support IP mode\n");
  261. return -EINVAL;
  262. }
  263. return 0;
  264. }
  265. #endif
  266. static struct xt_match l2tp_mt_reg[] __read_mostly = {
  267. {
  268. .name = "l2tp",
  269. .revision = 0,
  270. .family = NFPROTO_IPV4,
  271. .match = l2tp_mt4,
  272. .matchsize = XT_ALIGN(sizeof(struct xt_l2tp_info)),
  273. .checkentry = l2tp_mt_check4,
  274. .hooks = ((1 << NF_INET_PRE_ROUTING) |
  275. (1 << NF_INET_LOCAL_IN) |
  276. (1 << NF_INET_LOCAL_OUT) |
  277. (1 << NF_INET_FORWARD)),
  278. .me = THIS_MODULE,
  279. },
  280. #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
  281. {
  282. .name = "l2tp",
  283. .revision = 0,
  284. .family = NFPROTO_IPV6,
  285. .match = l2tp_mt6,
  286. .matchsize = XT_ALIGN(sizeof(struct xt_l2tp_info)),
  287. .checkentry = l2tp_mt_check6,
  288. .hooks = ((1 << NF_INET_PRE_ROUTING) |
  289. (1 << NF_INET_LOCAL_IN) |
  290. (1 << NF_INET_LOCAL_OUT) |
  291. (1 << NF_INET_FORWARD)),
  292. .me = THIS_MODULE,
  293. },
  294. #endif
  295. };
  296. static int __init l2tp_mt_init(void)
  297. {
  298. return xt_register_matches(&l2tp_mt_reg[0], ARRAY_SIZE(l2tp_mt_reg));
  299. }
  300. static void __exit l2tp_mt_exit(void)
  301. {
  302. xt_unregister_matches(&l2tp_mt_reg[0], ARRAY_SIZE(l2tp_mt_reg));
  303. }
  304. module_init(l2tp_mt_init);
  305. module_exit(l2tp_mt_exit);