act_csum.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  148. return 1;
  149. tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
  150. if (tcph == NULL)
  151. return 0;
  152. iph = ip_hdr(skb);
  153. tcph->check = 0;
  154. skb->csum = csum_partial(tcph, ipl - ihl, 0);
  155. tcph->check = tcp_v4_check(ipl - ihl,
  156. iph->saddr, iph->daddr, skb->csum);
  157. skb->ip_summed = CHECKSUM_NONE;
  158. return 1;
  159. }
  160. static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
  161. unsigned int ipl)
  162. {
  163. struct tcphdr *tcph;
  164. const struct ipv6hdr *ip6h;
  165. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  166. return 1;
  167. tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
  168. if (tcph == NULL)
  169. return 0;
  170. ip6h = ipv6_hdr(skb);
  171. tcph->check = 0;
  172. skb->csum = csum_partial(tcph, ipl - ihl, 0);
  173. tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  174. ipl - ihl, IPPROTO_TCP,
  175. skb->csum);
  176. skb->ip_summed = CHECKSUM_NONE;
  177. return 1;
  178. }
  179. static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
  180. unsigned int ipl, int udplite)
  181. {
  182. struct udphdr *udph;
  183. const struct iphdr *iph;
  184. u16 ul;
  185. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  186. return 1;
  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. iph = ip_hdr(skb);
  197. ul = ntohs(udph->len);
  198. if (udplite || udph->check) {
  199. udph->check = 0;
  200. if (udplite) {
  201. if (ul == 0)
  202. skb->csum = csum_partial(udph, ipl - ihl, 0);
  203. else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
  204. skb->csum = csum_partial(udph, ul, 0);
  205. else
  206. goto ignore_obscure_skb;
  207. } else {
  208. if (ul != ipl - ihl)
  209. goto ignore_obscure_skb;
  210. skb->csum = csum_partial(udph, ul, 0);
  211. }
  212. udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
  213. ul, iph->protocol,
  214. skb->csum);
  215. if (!udph->check)
  216. udph->check = CSUM_MANGLED_0;
  217. }
  218. skb->ip_summed = CHECKSUM_NONE;
  219. ignore_obscure_skb:
  220. return 1;
  221. }
  222. static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
  223. unsigned int ipl, int udplite)
  224. {
  225. struct udphdr *udph;
  226. const struct ipv6hdr *ip6h;
  227. u16 ul;
  228. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  229. return 1;
  230. /*
  231. * Support both UDP and UDPLITE checksum algorithms, Don't use
  232. * udph->len to get the real length without any protocol check,
  233. * UDPLITE uses udph->len for another thing,
  234. * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
  235. */
  236. udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
  237. if (udph == NULL)
  238. return 0;
  239. ip6h = ipv6_hdr(skb);
  240. ul = ntohs(udph->len);
  241. udph->check = 0;
  242. if (udplite) {
  243. if (ul == 0)
  244. skb->csum = csum_partial(udph, ipl - ihl, 0);
  245. else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
  246. skb->csum = csum_partial(udph, ul, 0);
  247. else
  248. goto ignore_obscure_skb;
  249. } else {
  250. if (ul != ipl - ihl)
  251. goto ignore_obscure_skb;
  252. skb->csum = csum_partial(udph, ul, 0);
  253. }
  254. udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
  255. udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
  256. skb->csum);
  257. if (!udph->check)
  258. udph->check = CSUM_MANGLED_0;
  259. skb->ip_summed = CHECKSUM_NONE;
  260. ignore_obscure_skb:
  261. return 1;
  262. }
  263. static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
  264. {
  265. const struct iphdr *iph;
  266. int ntkoff;
  267. ntkoff = skb_network_offset(skb);
  268. if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
  269. goto fail;
  270. iph = ip_hdr(skb);
  271. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  272. case IPPROTO_ICMP:
  273. if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
  274. if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
  275. ntohs(iph->tot_len)))
  276. goto fail;
  277. break;
  278. case IPPROTO_IGMP:
  279. if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
  280. if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
  281. ntohs(iph->tot_len)))
  282. goto fail;
  283. break;
  284. case IPPROTO_TCP:
  285. if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
  286. if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
  287. ntohs(iph->tot_len)))
  288. goto fail;
  289. break;
  290. case IPPROTO_UDP:
  291. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
  292. if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
  293. ntohs(iph->tot_len), 0))
  294. goto fail;
  295. break;
  296. case IPPROTO_UDPLITE:
  297. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
  298. if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
  299. ntohs(iph->tot_len), 1))
  300. goto fail;
  301. break;
  302. }
  303. if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
  304. if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
  305. goto fail;
  306. ip_send_check(ip_hdr(skb));
  307. }
  308. return 1;
  309. fail:
  310. return 0;
  311. }
  312. static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
  313. unsigned int *pl)
  314. {
  315. int off, len, optlen;
  316. unsigned char *xh = (void *)ip6xh;
  317. off = sizeof(*ip6xh);
  318. len = ixhl - off;
  319. while (len > 1) {
  320. switch (xh[off]) {
  321. case IPV6_TLV_PAD1:
  322. optlen = 1;
  323. break;
  324. case IPV6_TLV_JUMBO:
  325. optlen = xh[off + 1] + 2;
  326. if (optlen != 6 || len < 6 || (off & 3) != 2)
  327. /* wrong jumbo option length/alignment */
  328. return 0;
  329. *pl = ntohl(*(__be32 *)(xh + off + 2));
  330. goto done;
  331. default:
  332. optlen = xh[off + 1] + 2;
  333. if (optlen > len)
  334. /* ignore obscure options */
  335. goto done;
  336. break;
  337. }
  338. off += optlen;
  339. len -= optlen;
  340. }
  341. done:
  342. return 1;
  343. }
  344. static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
  345. {
  346. struct ipv6hdr *ip6h;
  347. struct ipv6_opt_hdr *ip6xh;
  348. unsigned int hl, ixhl;
  349. unsigned int pl;
  350. int ntkoff;
  351. u8 nexthdr;
  352. ntkoff = skb_network_offset(skb);
  353. hl = sizeof(*ip6h);
  354. if (!pskb_may_pull(skb, hl + ntkoff))
  355. goto fail;
  356. ip6h = ipv6_hdr(skb);
  357. pl = ntohs(ip6h->payload_len);
  358. nexthdr = ip6h->nexthdr;
  359. do {
  360. switch (nexthdr) {
  361. case NEXTHDR_FRAGMENT:
  362. goto ignore_skb;
  363. case NEXTHDR_ROUTING:
  364. case NEXTHDR_HOP:
  365. case NEXTHDR_DEST:
  366. if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
  367. goto fail;
  368. ip6xh = (void *)(skb_network_header(skb) + hl);
  369. ixhl = ipv6_optlen(ip6xh);
  370. if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
  371. goto fail;
  372. ip6xh = (void *)(skb_network_header(skb) + hl);
  373. if ((nexthdr == NEXTHDR_HOP) &&
  374. !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
  375. goto fail;
  376. nexthdr = ip6xh->nexthdr;
  377. hl += ixhl;
  378. break;
  379. case IPPROTO_ICMPV6:
  380. if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
  381. if (!tcf_csum_ipv6_icmp(skb,
  382. hl, pl + sizeof(*ip6h)))
  383. goto fail;
  384. goto done;
  385. case IPPROTO_TCP:
  386. if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
  387. if (!tcf_csum_ipv6_tcp(skb,
  388. hl, pl + sizeof(*ip6h)))
  389. goto fail;
  390. goto done;
  391. case IPPROTO_UDP:
  392. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
  393. if (!tcf_csum_ipv6_udp(skb, hl,
  394. pl + sizeof(*ip6h), 0))
  395. goto fail;
  396. goto done;
  397. case IPPROTO_UDPLITE:
  398. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
  399. if (!tcf_csum_ipv6_udp(skb, hl,
  400. pl + sizeof(*ip6h), 1))
  401. goto fail;
  402. goto done;
  403. default:
  404. goto ignore_skb;
  405. }
  406. } while (pskb_may_pull(skb, hl + 1 + ntkoff));
  407. done:
  408. ignore_skb:
  409. return 1;
  410. fail:
  411. return 0;
  412. }
  413. static int tcf_csum(struct sk_buff *skb, const struct tc_action *a,
  414. struct tcf_result *res)
  415. {
  416. struct tcf_csum *p = to_tcf_csum(a);
  417. int action;
  418. u32 update_flags;
  419. spin_lock(&p->tcf_lock);
  420. tcf_lastuse_update(&p->tcf_tm);
  421. bstats_update(&p->tcf_bstats, skb);
  422. action = p->tcf_action;
  423. update_flags = p->update_flags;
  424. spin_unlock(&p->tcf_lock);
  425. if (unlikely(action == TC_ACT_SHOT))
  426. goto drop;
  427. switch (tc_skb_protocol(skb)) {
  428. case cpu_to_be16(ETH_P_IP):
  429. if (!tcf_csum_ipv4(skb, update_flags))
  430. goto drop;
  431. break;
  432. case cpu_to_be16(ETH_P_IPV6):
  433. if (!tcf_csum_ipv6(skb, update_flags))
  434. goto drop;
  435. break;
  436. }
  437. return action;
  438. drop:
  439. spin_lock(&p->tcf_lock);
  440. p->tcf_qstats.drops++;
  441. spin_unlock(&p->tcf_lock);
  442. return TC_ACT_SHOT;
  443. }
  444. static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  445. int ref)
  446. {
  447. unsigned char *b = skb_tail_pointer(skb);
  448. struct tcf_csum *p = to_tcf_csum(a);
  449. struct tc_csum opt = {
  450. .update_flags = p->update_flags,
  451. .index = p->tcf_index,
  452. .action = p->tcf_action,
  453. .refcnt = p->tcf_refcnt - ref,
  454. .bindcnt = p->tcf_bindcnt - bind,
  455. };
  456. struct tcf_t t;
  457. if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
  458. goto nla_put_failure;
  459. tcf_tm_dump(&t, &p->tcf_tm);
  460. if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
  461. goto nla_put_failure;
  462. return skb->len;
  463. nla_put_failure:
  464. nlmsg_trim(skb, b);
  465. return -1;
  466. }
  467. static int tcf_csum_walker(struct net *net, struct sk_buff *skb,
  468. struct netlink_callback *cb, int type,
  469. const struct tc_action_ops *ops)
  470. {
  471. struct tc_action_net *tn = net_generic(net, csum_net_id);
  472. return tcf_generic_walker(tn, skb, cb, type, ops);
  473. }
  474. static int tcf_csum_search(struct net *net, struct tc_action **a, u32 index)
  475. {
  476. struct tc_action_net *tn = net_generic(net, csum_net_id);
  477. return tcf_hash_search(tn, a, index);
  478. }
  479. static struct tc_action_ops act_csum_ops = {
  480. .kind = "csum",
  481. .type = TCA_ACT_CSUM,
  482. .owner = THIS_MODULE,
  483. .act = tcf_csum,
  484. .dump = tcf_csum_dump,
  485. .init = tcf_csum_init,
  486. .walk = tcf_csum_walker,
  487. .lookup = tcf_csum_search,
  488. .size = sizeof(struct tcf_csum),
  489. };
  490. static __net_init int csum_init_net(struct net *net)
  491. {
  492. struct tc_action_net *tn = net_generic(net, csum_net_id);
  493. return tc_action_net_init(tn, &act_csum_ops, CSUM_TAB_MASK);
  494. }
  495. static void __net_exit csum_exit_net(struct net *net)
  496. {
  497. struct tc_action_net *tn = net_generic(net, csum_net_id);
  498. tc_action_net_exit(tn);
  499. }
  500. static struct pernet_operations csum_net_ops = {
  501. .init = csum_init_net,
  502. .exit = csum_exit_net,
  503. .id = &csum_net_id,
  504. .size = sizeof(struct tc_action_net),
  505. };
  506. MODULE_DESCRIPTION("Checksum updating actions");
  507. MODULE_LICENSE("GPL");
  508. static int __init csum_init_module(void)
  509. {
  510. return tcf_register_action(&act_csum_ops, &csum_net_ops);
  511. }
  512. static void __exit csum_cleanup_module(void)
  513. {
  514. tcf_unregister_action(&act_csum_ops, &csum_net_ops);
  515. }
  516. module_init(csum_init_module);
  517. module_exit(csum_cleanup_module);