act_csum.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. /*
  2. * Checksum updating actions
  3. *
  4. * Copyright (c) 2010 Gregoire Baron <baronchon@n7mm.org>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation; either version 2 of the License, or (at your option)
  9. * any later version.
  10. *
  11. */
  12. #include <linux/types.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/netlink.h>
  18. #include <net/netlink.h>
  19. #include <linux/rtnetlink.h>
  20. #include <linux/skbuff.h>
  21. #include <net/ip.h>
  22. #include <net/ipv6.h>
  23. #include <net/icmp.h>
  24. #include <linux/icmpv6.h>
  25. #include <linux/igmp.h>
  26. #include <net/tcp.h>
  27. #include <net/udp.h>
  28. #include <net/ip6_checksum.h>
  29. #include <net/act_api.h>
  30. #include <linux/tc_act/tc_csum.h>
  31. #include <net/tc_act/tc_csum.h>
  32. #define CSUM_TAB_MASK 15
  33. static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
  34. [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
  35. };
  36. static int tcf_csum_init(struct net *n, struct nlattr *nla, struct nlattr *est,
  37. struct tc_action *a, int ovr, int bind)
  38. {
  39. struct nlattr *tb[TCA_CSUM_MAX + 1];
  40. struct tc_csum *parm;
  41. struct tcf_csum *p;
  42. int ret = 0, err;
  43. if (nla == NULL)
  44. return -EINVAL;
  45. err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy);
  46. if (err < 0)
  47. return err;
  48. if (tb[TCA_CSUM_PARMS] == NULL)
  49. return -EINVAL;
  50. parm = nla_data(tb[TCA_CSUM_PARMS]);
  51. if (!tcf_hash_check(parm->index, a, bind)) {
  52. ret = tcf_hash_create(parm->index, est, a, sizeof(*p),
  53. bind, false);
  54. if (ret)
  55. return ret;
  56. ret = ACT_P_CREATED;
  57. } else {
  58. if (bind)/* dont override defaults */
  59. return 0;
  60. tcf_hash_release(a, bind);
  61. if (!ovr)
  62. return -EEXIST;
  63. }
  64. p = to_tcf_csum(a);
  65. spin_lock_bh(&p->tcf_lock);
  66. p->tcf_action = parm->action;
  67. p->update_flags = parm->update_flags;
  68. spin_unlock_bh(&p->tcf_lock);
  69. if (ret == ACT_P_CREATED)
  70. tcf_hash_insert(a);
  71. return ret;
  72. }
  73. /**
  74. * tcf_csum_skb_nextlayer - Get next layer pointer
  75. * @skb: sk_buff to use
  76. * @ihl: previous summed headers length
  77. * @ipl: complete packet length
  78. * @jhl: next header length
  79. *
  80. * Check the expected next layer availability in the specified sk_buff.
  81. * Return the next layer pointer if pass, NULL otherwise.
  82. */
  83. static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
  84. unsigned int ihl, unsigned int ipl,
  85. unsigned int jhl)
  86. {
  87. int ntkoff = skb_network_offset(skb);
  88. int hl = ihl + jhl;
  89. if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
  90. (skb_cloned(skb) &&
  91. !skb_clone_writable(skb, hl + ntkoff) &&
  92. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  93. return NULL;
  94. else
  95. return (void *)(skb_network_header(skb) + ihl);
  96. }
  97. static int tcf_csum_ipv4_icmp(struct sk_buff *skb,
  98. unsigned int ihl, unsigned int ipl)
  99. {
  100. struct icmphdr *icmph;
  101. icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
  102. if (icmph == NULL)
  103. return 0;
  104. icmph->checksum = 0;
  105. skb->csum = csum_partial(icmph, ipl - ihl, 0);
  106. icmph->checksum = csum_fold(skb->csum);
  107. skb->ip_summed = CHECKSUM_NONE;
  108. return 1;
  109. }
  110. static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
  111. unsigned int ihl, unsigned int ipl)
  112. {
  113. struct igmphdr *igmph;
  114. igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
  115. if (igmph == NULL)
  116. return 0;
  117. igmph->csum = 0;
  118. skb->csum = csum_partial(igmph, ipl - ihl, 0);
  119. igmph->csum = csum_fold(skb->csum);
  120. skb->ip_summed = CHECKSUM_NONE;
  121. return 1;
  122. }
  123. static int tcf_csum_ipv6_icmp(struct sk_buff *skb,
  124. unsigned int ihl, unsigned int ipl)
  125. {
  126. struct icmp6hdr *icmp6h;
  127. const struct ipv6hdr *ip6h;
  128. icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
  129. if (icmp6h == NULL)
  130. return 0;
  131. ip6h = ipv6_hdr(skb);
  132. icmp6h->icmp6_cksum = 0;
  133. skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
  134. icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  135. ipl - ihl, IPPROTO_ICMPV6,
  136. skb->csum);
  137. skb->ip_summed = CHECKSUM_NONE;
  138. return 1;
  139. }
  140. static int tcf_csum_ipv4_tcp(struct sk_buff *skb,
  141. unsigned int ihl, unsigned int ipl)
  142. {
  143. struct tcphdr *tcph;
  144. const struct iphdr *iph;
  145. tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
  146. if (tcph == NULL)
  147. return 0;
  148. iph = ip_hdr(skb);
  149. tcph->check = 0;
  150. skb->csum = csum_partial(tcph, ipl - ihl, 0);
  151. tcph->check = tcp_v4_check(ipl - ihl,
  152. iph->saddr, iph->daddr, skb->csum);
  153. skb->ip_summed = CHECKSUM_NONE;
  154. return 1;
  155. }
  156. static int tcf_csum_ipv6_tcp(struct sk_buff *skb,
  157. unsigned int ihl, unsigned int ipl)
  158. {
  159. struct tcphdr *tcph;
  160. const struct ipv6hdr *ip6h;
  161. tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
  162. if (tcph == NULL)
  163. return 0;
  164. ip6h = ipv6_hdr(skb);
  165. tcph->check = 0;
  166. skb->csum = csum_partial(tcph, ipl - ihl, 0);
  167. tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  168. ipl - ihl, IPPROTO_TCP,
  169. skb->csum);
  170. skb->ip_summed = CHECKSUM_NONE;
  171. return 1;
  172. }
  173. static int tcf_csum_ipv4_udp(struct sk_buff *skb,
  174. unsigned int ihl, unsigned int ipl, int udplite)
  175. {
  176. struct udphdr *udph;
  177. const struct iphdr *iph;
  178. u16 ul;
  179. /*
  180. * Support both UDP and UDPLITE checksum algorithms, Don't use
  181. * udph->len to get the real length without any protocol check,
  182. * UDPLITE uses udph->len for another thing,
  183. * Use iph->tot_len, or just ipl.
  184. */
  185. udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
  186. if (udph == NULL)
  187. return 0;
  188. iph = ip_hdr(skb);
  189. ul = ntohs(udph->len);
  190. if (udplite || udph->check) {
  191. udph->check = 0;
  192. if (udplite) {
  193. if (ul == 0)
  194. skb->csum = csum_partial(udph, ipl - ihl, 0);
  195. else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
  196. skb->csum = csum_partial(udph, ul, 0);
  197. else
  198. goto ignore_obscure_skb;
  199. } else {
  200. if (ul != ipl - ihl)
  201. goto ignore_obscure_skb;
  202. skb->csum = csum_partial(udph, ul, 0);
  203. }
  204. udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
  205. ul, iph->protocol,
  206. skb->csum);
  207. if (!udph->check)
  208. udph->check = CSUM_MANGLED_0;
  209. }
  210. skb->ip_summed = CHECKSUM_NONE;
  211. ignore_obscure_skb:
  212. return 1;
  213. }
  214. static int tcf_csum_ipv6_udp(struct sk_buff *skb,
  215. unsigned int ihl, unsigned int ipl, int udplite)
  216. {
  217. struct udphdr *udph;
  218. const struct ipv6hdr *ip6h;
  219. u16 ul;
  220. /*
  221. * Support both UDP and UDPLITE checksum algorithms, Don't use
  222. * udph->len to get the real length without any protocol check,
  223. * UDPLITE uses udph->len for another thing,
  224. * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
  225. */
  226. udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
  227. if (udph == NULL)
  228. return 0;
  229. ip6h = ipv6_hdr(skb);
  230. ul = ntohs(udph->len);
  231. udph->check = 0;
  232. if (udplite) {
  233. if (ul == 0)
  234. skb->csum = csum_partial(udph, ipl - ihl, 0);
  235. else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
  236. skb->csum = csum_partial(udph, ul, 0);
  237. else
  238. goto ignore_obscure_skb;
  239. } else {
  240. if (ul != ipl - ihl)
  241. goto ignore_obscure_skb;
  242. skb->csum = csum_partial(udph, ul, 0);
  243. }
  244. udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
  245. udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
  246. skb->csum);
  247. if (!udph->check)
  248. udph->check = CSUM_MANGLED_0;
  249. skb->ip_summed = CHECKSUM_NONE;
  250. ignore_obscure_skb:
  251. return 1;
  252. }
  253. static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
  254. {
  255. const struct iphdr *iph;
  256. int ntkoff;
  257. ntkoff = skb_network_offset(skb);
  258. if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
  259. goto fail;
  260. iph = ip_hdr(skb);
  261. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  262. case IPPROTO_ICMP:
  263. if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
  264. if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
  265. ntohs(iph->tot_len)))
  266. goto fail;
  267. break;
  268. case IPPROTO_IGMP:
  269. if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
  270. if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
  271. ntohs(iph->tot_len)))
  272. goto fail;
  273. break;
  274. case IPPROTO_TCP:
  275. if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
  276. if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
  277. ntohs(iph->tot_len)))
  278. goto fail;
  279. break;
  280. case IPPROTO_UDP:
  281. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
  282. if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
  283. ntohs(iph->tot_len), 0))
  284. goto fail;
  285. break;
  286. case IPPROTO_UDPLITE:
  287. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
  288. if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
  289. ntohs(iph->tot_len), 1))
  290. goto fail;
  291. break;
  292. }
  293. if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
  294. if (skb_cloned(skb) &&
  295. !skb_clone_writable(skb, sizeof(*iph) + ntkoff) &&
  296. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  297. goto fail;
  298. ip_send_check(ip_hdr(skb));
  299. }
  300. return 1;
  301. fail:
  302. return 0;
  303. }
  304. static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh,
  305. unsigned int ixhl, unsigned int *pl)
  306. {
  307. int off, len, optlen;
  308. unsigned char *xh = (void *)ip6xh;
  309. off = sizeof(*ip6xh);
  310. len = ixhl - off;
  311. while (len > 1) {
  312. switch (xh[off]) {
  313. case IPV6_TLV_PAD1:
  314. optlen = 1;
  315. break;
  316. case IPV6_TLV_JUMBO:
  317. optlen = xh[off + 1] + 2;
  318. if (optlen != 6 || len < 6 || (off & 3) != 2)
  319. /* wrong jumbo option length/alignment */
  320. return 0;
  321. *pl = ntohl(*(__be32 *)(xh + off + 2));
  322. goto done;
  323. default:
  324. optlen = xh[off + 1] + 2;
  325. if (optlen > len)
  326. /* ignore obscure options */
  327. goto done;
  328. break;
  329. }
  330. off += optlen;
  331. len -= optlen;
  332. }
  333. done:
  334. return 1;
  335. }
  336. static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
  337. {
  338. struct ipv6hdr *ip6h;
  339. struct ipv6_opt_hdr *ip6xh;
  340. unsigned int hl, ixhl;
  341. unsigned int pl;
  342. int ntkoff;
  343. u8 nexthdr;
  344. ntkoff = skb_network_offset(skb);
  345. hl = sizeof(*ip6h);
  346. if (!pskb_may_pull(skb, hl + ntkoff))
  347. goto fail;
  348. ip6h = ipv6_hdr(skb);
  349. pl = ntohs(ip6h->payload_len);
  350. nexthdr = ip6h->nexthdr;
  351. do {
  352. switch (nexthdr) {
  353. case NEXTHDR_FRAGMENT:
  354. goto ignore_skb;
  355. case NEXTHDR_ROUTING:
  356. case NEXTHDR_HOP:
  357. case NEXTHDR_DEST:
  358. if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
  359. goto fail;
  360. ip6xh = (void *)(skb_network_header(skb) + hl);
  361. ixhl = ipv6_optlen(ip6xh);
  362. if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
  363. goto fail;
  364. ip6xh = (void *)(skb_network_header(skb) + hl);
  365. if ((nexthdr == NEXTHDR_HOP) &&
  366. !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
  367. goto fail;
  368. nexthdr = ip6xh->nexthdr;
  369. hl += ixhl;
  370. break;
  371. case IPPROTO_ICMPV6:
  372. if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
  373. if (!tcf_csum_ipv6_icmp(skb,
  374. hl, pl + sizeof(*ip6h)))
  375. goto fail;
  376. goto done;
  377. case IPPROTO_TCP:
  378. if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
  379. if (!tcf_csum_ipv6_tcp(skb,
  380. hl, pl + sizeof(*ip6h)))
  381. goto fail;
  382. goto done;
  383. case IPPROTO_UDP:
  384. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
  385. if (!tcf_csum_ipv6_udp(skb, hl,
  386. pl + sizeof(*ip6h), 0))
  387. goto fail;
  388. goto done;
  389. case IPPROTO_UDPLITE:
  390. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
  391. if (!tcf_csum_ipv6_udp(skb, hl,
  392. pl + sizeof(*ip6h), 1))
  393. goto fail;
  394. goto done;
  395. default:
  396. goto ignore_skb;
  397. }
  398. } while (pskb_may_pull(skb, hl + 1 + ntkoff));
  399. done:
  400. ignore_skb:
  401. return 1;
  402. fail:
  403. return 0;
  404. }
  405. static int tcf_csum(struct sk_buff *skb,
  406. const struct tc_action *a, struct tcf_result *res)
  407. {
  408. struct tcf_csum *p = a->priv;
  409. int action;
  410. u32 update_flags;
  411. spin_lock(&p->tcf_lock);
  412. p->tcf_tm.lastuse = jiffies;
  413. bstats_update(&p->tcf_bstats, skb);
  414. action = p->tcf_action;
  415. update_flags = p->update_flags;
  416. spin_unlock(&p->tcf_lock);
  417. if (unlikely(action == TC_ACT_SHOT))
  418. goto drop;
  419. switch (tc_skb_protocol(skb)) {
  420. case cpu_to_be16(ETH_P_IP):
  421. if (!tcf_csum_ipv4(skb, update_flags))
  422. goto drop;
  423. break;
  424. case cpu_to_be16(ETH_P_IPV6):
  425. if (!tcf_csum_ipv6(skb, update_flags))
  426. goto drop;
  427. break;
  428. }
  429. return action;
  430. drop:
  431. spin_lock(&p->tcf_lock);
  432. p->tcf_qstats.drops++;
  433. spin_unlock(&p->tcf_lock);
  434. return TC_ACT_SHOT;
  435. }
  436. static int tcf_csum_dump(struct sk_buff *skb,
  437. struct tc_action *a, int bind, int ref)
  438. {
  439. unsigned char *b = skb_tail_pointer(skb);
  440. struct tcf_csum *p = a->priv;
  441. struct tc_csum opt = {
  442. .update_flags = p->update_flags,
  443. .index = p->tcf_index,
  444. .action = p->tcf_action,
  445. .refcnt = p->tcf_refcnt - ref,
  446. .bindcnt = p->tcf_bindcnt - bind,
  447. };
  448. struct tcf_t t;
  449. if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
  450. goto nla_put_failure;
  451. t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
  452. t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
  453. t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
  454. if (nla_put(skb, TCA_CSUM_TM, sizeof(t), &t))
  455. goto nla_put_failure;
  456. return skb->len;
  457. nla_put_failure:
  458. nlmsg_trim(skb, b);
  459. return -1;
  460. }
  461. static struct tc_action_ops act_csum_ops = {
  462. .kind = "csum",
  463. .type = TCA_ACT_CSUM,
  464. .owner = THIS_MODULE,
  465. .act = tcf_csum,
  466. .dump = tcf_csum_dump,
  467. .init = tcf_csum_init,
  468. };
  469. MODULE_DESCRIPTION("Checksum updating actions");
  470. MODULE_LICENSE("GPL");
  471. static int __init csum_init_module(void)
  472. {
  473. return tcf_register_action(&act_csum_ops, CSUM_TAB_MASK);
  474. }
  475. static void __exit csum_cleanup_module(void)
  476. {
  477. tcf_unregister_action(&act_csum_ops);
  478. }
  479. module_init(csum_init_module);
  480. module_exit(csum_cleanup_module);