act_csum.c 13 KB

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