act_csum.c 13 KB

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