act_csum.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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,
  63. &csum_idx_gen, &csum_hash_info);
  64. if (IS_ERR(pc))
  65. return PTR_ERR(pc);
  66. p = to_tcf_csum(pc);
  67. ret = ACT_P_CREATED;
  68. } else {
  69. p = to_tcf_csum(pc);
  70. if (!ovr) {
  71. tcf_hash_release(pc, bind, &csum_hash_info);
  72. return -EEXIST;
  73. }
  74. }
  75. spin_lock_bh(&p->tcf_lock);
  76. p->tcf_action = parm->action;
  77. p->update_flags = parm->update_flags;
  78. spin_unlock_bh(&p->tcf_lock);
  79. if (ret == ACT_P_CREATED)
  80. tcf_hash_insert(pc, &csum_hash_info);
  81. return ret;
  82. }
  83. static int tcf_csum_cleanup(struct tc_action *a, int bind)
  84. {
  85. struct tcf_csum *p = a->priv;
  86. return tcf_hash_release(&p->common, bind, &csum_hash_info);
  87. }
  88. /**
  89. * tcf_csum_skb_nextlayer - Get next layer pointer
  90. * @skb: sk_buff to use
  91. * @ihl: previous summed headers length
  92. * @ipl: complete packet length
  93. * @jhl: next header length
  94. *
  95. * Check the expected next layer availability in the specified sk_buff.
  96. * Return the next layer pointer if pass, NULL otherwise.
  97. */
  98. static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
  99. unsigned int ihl, unsigned int ipl,
  100. unsigned int jhl)
  101. {
  102. int ntkoff = skb_network_offset(skb);
  103. int hl = ihl + jhl;
  104. if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
  105. (skb_cloned(skb) &&
  106. !skb_clone_writable(skb, hl + ntkoff) &&
  107. pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
  108. return NULL;
  109. else
  110. return (void *)(skb_network_header(skb) + ihl);
  111. }
  112. static int tcf_csum_ipv4_icmp(struct sk_buff *skb,
  113. unsigned int ihl, unsigned int ipl)
  114. {
  115. struct icmphdr *icmph;
  116. icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
  117. if (icmph == NULL)
  118. return 0;
  119. icmph->checksum = 0;
  120. skb->csum = csum_partial(icmph, ipl - ihl, 0);
  121. icmph->checksum = csum_fold(skb->csum);
  122. skb->ip_summed = CHECKSUM_NONE;
  123. return 1;
  124. }
  125. static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
  126. unsigned int ihl, unsigned int ipl)
  127. {
  128. struct igmphdr *igmph;
  129. igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
  130. if (igmph == NULL)
  131. return 0;
  132. igmph->csum = 0;
  133. skb->csum = csum_partial(igmph, ipl - ihl, 0);
  134. igmph->csum = csum_fold(skb->csum);
  135. skb->ip_summed = CHECKSUM_NONE;
  136. return 1;
  137. }
  138. static int tcf_csum_ipv6_icmp(struct sk_buff *skb, struct ipv6hdr *ip6h,
  139. unsigned int ihl, unsigned int ipl)
  140. {
  141. struct icmp6hdr *icmp6h;
  142. icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
  143. if (icmp6h == NULL)
  144. return 0;
  145. icmp6h->icmp6_cksum = 0;
  146. skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
  147. icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  148. ipl - ihl, IPPROTO_ICMPV6,
  149. skb->csum);
  150. skb->ip_summed = CHECKSUM_NONE;
  151. return 1;
  152. }
  153. static int tcf_csum_ipv4_tcp(struct sk_buff *skb, struct iphdr *iph,
  154. unsigned int ihl, unsigned int ipl)
  155. {
  156. struct tcphdr *tcph;
  157. tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
  158. if (tcph == NULL)
  159. return 0;
  160. tcph->check = 0;
  161. skb->csum = csum_partial(tcph, ipl - ihl, 0);
  162. tcph->check = tcp_v4_check(ipl - ihl,
  163. iph->saddr, iph->daddr, skb->csum);
  164. skb->ip_summed = CHECKSUM_NONE;
  165. return 1;
  166. }
  167. static int tcf_csum_ipv6_tcp(struct sk_buff *skb, struct ipv6hdr *ip6h,
  168. unsigned int ihl, unsigned int ipl)
  169. {
  170. struct tcphdr *tcph;
  171. tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
  172. if (tcph == NULL)
  173. return 0;
  174. tcph->check = 0;
  175. skb->csum = csum_partial(tcph, ipl - ihl, 0);
  176. tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  177. ipl - ihl, IPPROTO_TCP,
  178. skb->csum);
  179. skb->ip_summed = CHECKSUM_NONE;
  180. return 1;
  181. }
  182. static int tcf_csum_ipv4_udp(struct sk_buff *skb, struct iphdr *iph,
  183. unsigned int ihl, unsigned int ipl, int udplite)
  184. {
  185. struct udphdr *udph;
  186. u16 ul;
  187. /*
  188. * Support both UDP and UDPLITE checksum algorithms, Don't use
  189. * udph->len to get the real length without any protocol check,
  190. * UDPLITE uses udph->len for another thing,
  191. * Use iph->tot_len, or just ipl.
  192. */
  193. udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
  194. if (udph == NULL)
  195. return 0;
  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, struct ipv6hdr *ip6h,
  222. unsigned int ihl, unsigned int ipl, int udplite)
  223. {
  224. struct udphdr *udph;
  225. u16 ul;
  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. ul = ntohs(udph->len);
  236. udph->check = 0;
  237. if (udplite) {
  238. if (ul == 0)
  239. skb->csum = csum_partial(udph, ipl - ihl, 0);
  240. else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
  241. skb->csum = csum_partial(udph, ul, 0);
  242. else
  243. goto ignore_obscure_skb;
  244. } else {
  245. if (ul != ipl - ihl)
  246. goto ignore_obscure_skb;
  247. skb->csum = csum_partial(udph, ul, 0);
  248. }
  249. udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
  250. udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
  251. skb->csum);
  252. if (!udph->check)
  253. udph->check = CSUM_MANGLED_0;
  254. skb->ip_summed = CHECKSUM_NONE;
  255. ignore_obscure_skb:
  256. return 1;
  257. }
  258. static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
  259. {
  260. struct iphdr *iph;
  261. int ntkoff;
  262. ntkoff = skb_network_offset(skb);
  263. if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
  264. goto fail;
  265. iph = ip_hdr(skb);
  266. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  267. case IPPROTO_ICMP:
  268. if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
  269. if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
  270. ntohs(iph->tot_len)))
  271. goto fail;
  272. break;
  273. case IPPROTO_IGMP:
  274. if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
  275. if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
  276. ntohs(iph->tot_len)))
  277. goto fail;
  278. break;
  279. case IPPROTO_TCP:
  280. if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
  281. if (!tcf_csum_ipv4_tcp(skb, iph, iph->ihl * 4,
  282. ntohs(iph->tot_len)))
  283. goto fail;
  284. break;
  285. case IPPROTO_UDP:
  286. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
  287. if (!tcf_csum_ipv4_udp(skb, iph, iph->ihl * 4,
  288. ntohs(iph->tot_len), 0))
  289. goto fail;
  290. break;
  291. case IPPROTO_UDPLITE:
  292. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
  293. if (!tcf_csum_ipv4_udp(skb, iph, iph->ihl * 4,
  294. ntohs(iph->tot_len), 1))
  295. goto fail;
  296. break;
  297. }
  298. if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
  299. if (skb_cloned(skb) &&
  300. !skb_clone_writable(skb, sizeof(*iph) + ntkoff) &&
  301. pskb_expand_head(skb, 0, 0, GFP_ATOMIC))
  302. goto fail;
  303. ip_send_check(iph);
  304. }
  305. return 1;
  306. fail:
  307. return 0;
  308. }
  309. static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh,
  310. unsigned int ixhl, unsigned int *pl)
  311. {
  312. int off, len, optlen;
  313. unsigned char *xh = (void *)ip6xh;
  314. off = sizeof(*ip6xh);
  315. len = ixhl - off;
  316. while (len > 1) {
  317. switch (xh[off]) {
  318. case IPV6_TLV_PAD0:
  319. optlen = 1;
  320. break;
  321. case IPV6_TLV_JUMBO:
  322. optlen = xh[off + 1] + 2;
  323. if (optlen != 6 || len < 6 || (off & 3) != 2)
  324. /* wrong jumbo option length/alignment */
  325. return 0;
  326. *pl = ntohl(*(__be32 *)(xh + off + 2));
  327. goto done;
  328. default:
  329. optlen = xh[off + 1] + 2;
  330. if (optlen > len)
  331. /* ignore obscure options */
  332. goto done;
  333. break;
  334. }
  335. off += optlen;
  336. len -= optlen;
  337. }
  338. done:
  339. return 1;
  340. }
  341. static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
  342. {
  343. struct ipv6hdr *ip6h;
  344. struct ipv6_opt_hdr *ip6xh;
  345. unsigned int hl, ixhl;
  346. unsigned int pl;
  347. int ntkoff;
  348. u8 nexthdr;
  349. ntkoff = skb_network_offset(skb);
  350. hl = sizeof(*ip6h);
  351. if (!pskb_may_pull(skb, hl + ntkoff))
  352. goto fail;
  353. ip6h = ipv6_hdr(skb);
  354. pl = ntohs(ip6h->payload_len);
  355. nexthdr = ip6h->nexthdr;
  356. do {
  357. switch (nexthdr) {
  358. case NEXTHDR_FRAGMENT:
  359. goto ignore_skb;
  360. case NEXTHDR_ROUTING:
  361. case NEXTHDR_HOP:
  362. case NEXTHDR_DEST:
  363. if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
  364. goto fail;
  365. ip6xh = (void *)(skb_network_header(skb) + hl);
  366. ixhl = ipv6_optlen(ip6xh);
  367. if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
  368. goto fail;
  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, ip6h,
  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, ip6h,
  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, ip6h, 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, ip6h, 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 (skb->protocol) {
  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. NLA_PUT(skb, TCA_CSUM_PARMS, sizeof(opt), &opt);
  454. t.install = jiffies_to_clock_t(jiffies - p->tcf_tm.install);
  455. t.lastuse = jiffies_to_clock_t(jiffies - p->tcf_tm.lastuse);
  456. t.expires = jiffies_to_clock_t(p->tcf_tm.expires);
  457. NLA_PUT(skb, TCA_CSUM_TM, sizeof(t), &t);
  458. return skb->len;
  459. nla_put_failure:
  460. nlmsg_trim(skb, b);
  461. return -1;
  462. }
  463. static struct tc_action_ops act_csum_ops = {
  464. .kind = "csum",
  465. .hinfo = &csum_hash_info,
  466. .type = TCA_ACT_CSUM,
  467. .capab = TCA_CAP_NONE,
  468. .owner = THIS_MODULE,
  469. .act = tcf_csum,
  470. .dump = tcf_csum_dump,
  471. .cleanup = tcf_csum_cleanup,
  472. .lookup = tcf_hash_search,
  473. .init = tcf_csum_init,
  474. .walk = tcf_generic_walker
  475. };
  476. MODULE_DESCRIPTION("Checksum updating actions");
  477. MODULE_LICENSE("GPL");
  478. static int __init csum_init_module(void)
  479. {
  480. return tcf_register_action(&act_csum_ops);
  481. }
  482. static void __exit csum_cleanup_module(void)
  483. {
  484. tcf_unregister_action(&act_csum_ops);
  485. }
  486. module_init(csum_init_module);
  487. module_exit(csum_cleanup_module);