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