act_csum.c 13 KB

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