act_csum.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 csum_net_id;
  37. static struct tc_action_ops act_csum_ops;
  38. static int tcf_csum_init(struct net *net, struct nlattr *nla,
  39. struct nlattr *est, struct tc_action **a, int ovr,
  40. int bind)
  41. {
  42. struct tc_action_net *tn = net_generic(net, csum_net_id);
  43. struct nlattr *tb[TCA_CSUM_MAX + 1];
  44. struct tc_csum *parm;
  45. struct tcf_csum *p;
  46. int ret = 0, err;
  47. if (nla == NULL)
  48. return -EINVAL;
  49. err = nla_parse_nested(tb, TCA_CSUM_MAX, nla, csum_policy);
  50. if (err < 0)
  51. return err;
  52. if (tb[TCA_CSUM_PARMS] == NULL)
  53. return -EINVAL;
  54. parm = nla_data(tb[TCA_CSUM_PARMS]);
  55. if (!tcf_hash_check(tn, parm->index, a, bind)) {
  56. ret = tcf_hash_create(tn, parm->index, est, a,
  57. &act_csum_ops, bind, false);
  58. if (ret)
  59. return ret;
  60. ret = ACT_P_CREATED;
  61. } else {
  62. if (bind)/* dont override defaults */
  63. return 0;
  64. tcf_hash_release(*a, bind);
  65. if (!ovr)
  66. return -EEXIST;
  67. }
  68. p = to_tcf_csum(*a);
  69. spin_lock_bh(&p->tcf_lock);
  70. p->tcf_action = parm->action;
  71. p->update_flags = parm->update_flags;
  72. spin_unlock_bh(&p->tcf_lock);
  73. if (ret == ACT_P_CREATED)
  74. tcf_hash_insert(tn, *a);
  75. return ret;
  76. }
  77. /**
  78. * tcf_csum_skb_nextlayer - Get next layer pointer
  79. * @skb: sk_buff to use
  80. * @ihl: previous summed headers length
  81. * @ipl: complete packet length
  82. * @jhl: next header length
  83. *
  84. * Check the expected next layer availability in the specified sk_buff.
  85. * Return the next layer pointer if pass, NULL otherwise.
  86. */
  87. static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
  88. unsigned int ihl, unsigned int ipl,
  89. unsigned int jhl)
  90. {
  91. int ntkoff = skb_network_offset(skb);
  92. int hl = ihl + jhl;
  93. if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
  94. skb_try_make_writable(skb, hl + ntkoff))
  95. return NULL;
  96. else
  97. return (void *)(skb_network_header(skb) + ihl);
  98. }
  99. static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
  100. unsigned int ipl)
  101. {
  102. struct icmphdr *icmph;
  103. icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
  104. if (icmph == NULL)
  105. return 0;
  106. icmph->checksum = 0;
  107. skb->csum = csum_partial(icmph, ipl - ihl, 0);
  108. icmph->checksum = csum_fold(skb->csum);
  109. skb->ip_summed = CHECKSUM_NONE;
  110. return 1;
  111. }
  112. static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
  113. unsigned int ihl, unsigned int ipl)
  114. {
  115. struct igmphdr *igmph;
  116. igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
  117. if (igmph == NULL)
  118. return 0;
  119. igmph->csum = 0;
  120. skb->csum = csum_partial(igmph, ipl - ihl, 0);
  121. igmph->csum = csum_fold(skb->csum);
  122. skb->ip_summed = CHECKSUM_NONE;
  123. return 1;
  124. }
  125. static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
  126. unsigned int ipl)
  127. {
  128. struct icmp6hdr *icmp6h;
  129. const struct ipv6hdr *ip6h;
  130. icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
  131. if (icmp6h == NULL)
  132. return 0;
  133. ip6h = ipv6_hdr(skb);
  134. icmp6h->icmp6_cksum = 0;
  135. skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
  136. icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  137. ipl - ihl, IPPROTO_ICMPV6,
  138. skb->csum);
  139. skb->ip_summed = CHECKSUM_NONE;
  140. return 1;
  141. }
  142. static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
  143. unsigned int ipl)
  144. {
  145. struct tcphdr *tcph;
  146. const struct iphdr *iph;
  147. tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
  148. if (tcph == NULL)
  149. return 0;
  150. iph = ip_hdr(skb);
  151. tcph->check = 0;
  152. skb->csum = csum_partial(tcph, ipl - ihl, 0);
  153. tcph->check = tcp_v4_check(ipl - ihl,
  154. iph->saddr, iph->daddr, skb->csum);
  155. skb->ip_summed = CHECKSUM_NONE;
  156. return 1;
  157. }
  158. static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
  159. unsigned int ipl)
  160. {
  161. struct tcphdr *tcph;
  162. const struct ipv6hdr *ip6h;
  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, unsigned int ihl,
  176. unsigned int ipl, int udplite)
  177. {
  178. struct udphdr *udph;
  179. const struct iphdr *iph;
  180. u16 ul;
  181. /*
  182. * Support both UDP and UDPLITE checksum algorithms, Don't use
  183. * udph->len to get the real length without any protocol check,
  184. * UDPLITE uses udph->len for another thing,
  185. * Use iph->tot_len, or just ipl.
  186. */
  187. udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
  188. if (udph == NULL)
  189. return 0;
  190. iph = ip_hdr(skb);
  191. ul = ntohs(udph->len);
  192. if (udplite || udph->check) {
  193. udph->check = 0;
  194. if (udplite) {
  195. if (ul == 0)
  196. skb->csum = csum_partial(udph, ipl - ihl, 0);
  197. else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
  198. skb->csum = csum_partial(udph, ul, 0);
  199. else
  200. goto ignore_obscure_skb;
  201. } else {
  202. if (ul != ipl - ihl)
  203. goto ignore_obscure_skb;
  204. skb->csum = csum_partial(udph, ul, 0);
  205. }
  206. udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
  207. ul, iph->protocol,
  208. skb->csum);
  209. if (!udph->check)
  210. udph->check = CSUM_MANGLED_0;
  211. }
  212. skb->ip_summed = CHECKSUM_NONE;
  213. ignore_obscure_skb:
  214. return 1;
  215. }
  216. static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
  217. unsigned int ipl, int udplite)
  218. {
  219. struct udphdr *udph;
  220. const struct ipv6hdr *ip6h;
  221. u16 ul;
  222. /*
  223. * Support both UDP and UDPLITE checksum algorithms, Don't use
  224. * udph->len to get the real length without any protocol check,
  225. * UDPLITE uses udph->len for another thing,
  226. * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
  227. */
  228. udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
  229. if (udph == NULL)
  230. return 0;
  231. ip6h = ipv6_hdr(skb);
  232. ul = ntohs(udph->len);
  233. udph->check = 0;
  234. if (udplite) {
  235. if (ul == 0)
  236. skb->csum = csum_partial(udph, ipl - ihl, 0);
  237. else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
  238. skb->csum = csum_partial(udph, ul, 0);
  239. else
  240. goto ignore_obscure_skb;
  241. } else {
  242. if (ul != ipl - ihl)
  243. goto ignore_obscure_skb;
  244. skb->csum = csum_partial(udph, ul, 0);
  245. }
  246. udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
  247. udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
  248. skb->csum);
  249. if (!udph->check)
  250. udph->check = CSUM_MANGLED_0;
  251. skb->ip_summed = CHECKSUM_NONE;
  252. ignore_obscure_skb:
  253. return 1;
  254. }
  255. static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
  256. {
  257. const struct iphdr *iph;
  258. int ntkoff;
  259. ntkoff = skb_network_offset(skb);
  260. if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
  261. goto fail;
  262. iph = ip_hdr(skb);
  263. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  264. case IPPROTO_ICMP:
  265. if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
  266. if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
  267. ntohs(iph->tot_len)))
  268. goto fail;
  269. break;
  270. case IPPROTO_IGMP:
  271. if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
  272. if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
  273. ntohs(iph->tot_len)))
  274. goto fail;
  275. break;
  276. case IPPROTO_TCP:
  277. if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
  278. if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
  279. ntohs(iph->tot_len)))
  280. goto fail;
  281. break;
  282. case IPPROTO_UDP:
  283. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
  284. if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
  285. ntohs(iph->tot_len), 0))
  286. goto fail;
  287. break;
  288. case IPPROTO_UDPLITE:
  289. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
  290. if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
  291. ntohs(iph->tot_len), 1))
  292. goto fail;
  293. break;
  294. }
  295. if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
  296. if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
  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, unsigned int ixhl,
  305. 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, const struct tc_action *a,
  406. struct tcf_result *res)
  407. {
  408. struct tcf_csum *p = to_tcf_csum(a);
  409. int action;
  410. u32 update_flags;
  411. spin_lock(&p->tcf_lock);
  412. tcf_lastuse_update(&p->tcf_tm);
  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, struct tc_action *a, int bind,
  437. int ref)
  438. {
  439. unsigned char *b = skb_tail_pointer(skb);
  440. struct tcf_csum *p = to_tcf_csum(a);
  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. tcf_tm_dump(&t, &p->tcf_tm);
  452. if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
  453. goto nla_put_failure;
  454. return skb->len;
  455. nla_put_failure:
  456. nlmsg_trim(skb, b);
  457. return -1;
  458. }
  459. static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
  460. struct netlink_callback *cb, int type,
  461. const struct tc_action_ops *ops)
  462. {
  463. struct tc_action_net *tn = net_generic(net, csum_net_id);
  464. return tcf_generic_walker(tn, skb, cb, type, ops);
  465. }
  466. static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index)
  467. {
  468. struct tc_action_net *tn = net_generic(net, csum_net_id);
  469. return tcf_hash_search(tn, a, index);
  470. }
  471. static struct tc_action_ops act_csum_ops = {
  472. .kind = "csum",
  473. .type = TCA_ACT_CSUM,
  474. .owner = THIS_MODULE,
  475. .act = tcf_csum,
  476. .dump = tcf_csum_dump,
  477. .init = tcf_csum_init,
  478. .walk = tcf_csum_walker,
  479. .lookup = tcf_csum_search,
  480. .size = sizeof(struct tcf_csum),
  481. };
  482. static __net_init int csum_init_net(struct net *net)
  483. {
  484. struct tc_action_net *tn = net_generic(net, csum_net_id);
  485. return tc_action_net_init(tn, &act_csum_ops, CSUM_TAB_MASK);
  486. }
  487. static void __net_exit csum_exit_net(struct net *net)
  488. {
  489. struct tc_action_net *tn = net_generic(net, csum_net_id);
  490. tc_action_net_exit(tn);
  491. }
  492. static struct pernet_operations csum_net_ops = {
  493. .init = csum_init_net,
  494. .exit = csum_exit_net,
  495. .id = &csum_net_id,
  496. .size = sizeof(struct tc_action_net),
  497. };
  498. MODULE_DESCRIPTION("Checksum updating actions");
  499. MODULE_LICENSE("GPL");
  500. static int __init csum_init_module(void)
  501. {
  502. return tcf_register_action(&act_csum_ops, &csum_net_ops);
  503. }
  504. static void __exit csum_cleanup_module(void)
  505. {
  506. tcf_unregister_action(&act_csum_ops, &csum_net_ops);
  507. }
  508. module_init(csum_init_module);
  509. module_exit(csum_cleanup_module);