act_csum.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Checksum updating actions
  4. *
  5. * Copyright (c) 2010 Gregoire Baron <baronchon@n7mm.org>
  6. */
  7. #include <linux/types.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/netlink.h>
  13. #include <net/netlink.h>
  14. #include <linux/rtnetlink.h>
  15. #include <linux/skbuff.h>
  16. #include <net/ip.h>
  17. #include <net/ipv6.h>
  18. #include <net/icmp.h>
  19. #include <linux/icmpv6.h>
  20. #include <linux/igmp.h>
  21. #include <net/tcp.h>
  22. #include <net/udp.h>
  23. #include <net/ip6_checksum.h>
  24. #include <net/sctp/checksum.h>
  25. #include <net/act_api.h>
  26. #include <net/pkt_cls.h>
  27. #include <linux/tc_act/tc_csum.h>
  28. #include <net/tc_act/tc_csum.h>
  29. #include <net/tc_wrapper.h>
  30. static const struct nla_policy csum_policy[TCA_CSUM_MAX + 1] = {
  31. [TCA_CSUM_PARMS] = { .len = sizeof(struct tc_csum), },
  32. };
  33. static struct tc_action_ops act_csum_ops;
  34. static int tcf_csum_init(struct net *net, struct nlattr *nla,
  35. struct nlattr *est, struct tc_action **a,
  36. struct tcf_proto *tp,
  37. u32 flags, struct netlink_ext_ack *extack)
  38. {
  39. struct tc_action_net *tn = net_generic(net, act_csum_ops.net_id);
  40. bool bind = flags & TCA_ACT_FLAGS_BIND;
  41. struct tcf_csum_params *params_new;
  42. struct nlattr *tb[TCA_CSUM_MAX + 1];
  43. struct tcf_chain *goto_ch = NULL;
  44. struct tc_csum *parm;
  45. struct tcf_csum *p;
  46. int ret = 0, err;
  47. u32 index;
  48. if (nla == NULL)
  49. return -EINVAL;
  50. err = nla_parse_nested_deprecated(tb, TCA_CSUM_MAX, nla, csum_policy,
  51. NULL);
  52. if (err < 0)
  53. return err;
  54. if (tb[TCA_CSUM_PARMS] == NULL)
  55. return -EINVAL;
  56. parm = nla_data(tb[TCA_CSUM_PARMS]);
  57. index = parm->index;
  58. err = tcf_idr_check_alloc(tn, &index, a, bind);
  59. if (!err) {
  60. ret = tcf_idr_create_from_flags(tn, index, est, a,
  61. &act_csum_ops, bind, flags);
  62. if (ret) {
  63. tcf_idr_cleanup(tn, index);
  64. return ret;
  65. }
  66. ret = ACT_P_CREATED;
  67. } else if (err > 0) {
  68. if (bind) /* dont override defaults */
  69. return ACT_P_BOUND;
  70. if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
  71. tcf_idr_release(*a, bind);
  72. return -EEXIST;
  73. }
  74. } else {
  75. return err;
  76. }
  77. err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
  78. if (err < 0)
  79. goto release_idr;
  80. p = to_tcf_csum(*a);
  81. params_new = kzalloc_obj(*params_new);
  82. if (unlikely(!params_new)) {
  83. err = -ENOMEM;
  84. goto put_chain;
  85. }
  86. params_new->update_flags = parm->update_flags;
  87. params_new->action = parm->action;
  88. spin_lock_bh(&p->tcf_lock);
  89. goto_ch = tcf_action_set_ctrlact(*a, parm->action, goto_ch);
  90. params_new = rcu_replace_pointer(p->params, params_new,
  91. lockdep_is_held(&p->tcf_lock));
  92. spin_unlock_bh(&p->tcf_lock);
  93. if (goto_ch)
  94. tcf_chain_put_by_act(goto_ch);
  95. if (params_new)
  96. kfree_rcu(params_new, rcu);
  97. return ret;
  98. put_chain:
  99. if (goto_ch)
  100. tcf_chain_put_by_act(goto_ch);
  101. release_idr:
  102. tcf_idr_release(*a, bind);
  103. return err;
  104. }
  105. /**
  106. * tcf_csum_skb_nextlayer - Get next layer pointer
  107. * @skb: sk_buff to use
  108. * @ihl: previous summed headers length
  109. * @ipl: complete packet length
  110. * @jhl: next header length
  111. *
  112. * Check the expected next layer availability in the specified sk_buff.
  113. * Return the next layer pointer if pass, NULL otherwise.
  114. */
  115. static void *tcf_csum_skb_nextlayer(struct sk_buff *skb,
  116. unsigned int ihl, unsigned int ipl,
  117. unsigned int jhl)
  118. {
  119. int ntkoff = skb_network_offset(skb);
  120. int hl = ihl + jhl;
  121. if (!pskb_may_pull(skb, ipl + ntkoff) || (ipl < hl) ||
  122. skb_try_make_writable(skb, hl + ntkoff))
  123. return NULL;
  124. else
  125. return (void *)(skb_network_header(skb) + ihl);
  126. }
  127. static int tcf_csum_ipv4_icmp(struct sk_buff *skb, unsigned int ihl,
  128. unsigned int ipl)
  129. {
  130. struct icmphdr *icmph;
  131. icmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmph));
  132. if (icmph == NULL)
  133. return 0;
  134. icmph->checksum = 0;
  135. skb->csum = csum_partial(icmph, ipl - ihl, 0);
  136. icmph->checksum = csum_fold(skb->csum);
  137. skb->ip_summed = CHECKSUM_NONE;
  138. return 1;
  139. }
  140. static int tcf_csum_ipv4_igmp(struct sk_buff *skb,
  141. unsigned int ihl, unsigned int ipl)
  142. {
  143. struct igmphdr *igmph;
  144. igmph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*igmph));
  145. if (igmph == NULL)
  146. return 0;
  147. igmph->csum = 0;
  148. skb->csum = csum_partial(igmph, ipl - ihl, 0);
  149. igmph->csum = csum_fold(skb->csum);
  150. skb->ip_summed = CHECKSUM_NONE;
  151. return 1;
  152. }
  153. static int tcf_csum_ipv6_icmp(struct sk_buff *skb, unsigned int ihl,
  154. unsigned int ipl)
  155. {
  156. struct icmp6hdr *icmp6h;
  157. const struct ipv6hdr *ip6h;
  158. icmp6h = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*icmp6h));
  159. if (icmp6h == NULL)
  160. return 0;
  161. ip6h = ipv6_hdr(skb);
  162. icmp6h->icmp6_cksum = 0;
  163. skb->csum = csum_partial(icmp6h, ipl - ihl, 0);
  164. icmp6h->icmp6_cksum = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  165. ipl - ihl, IPPROTO_ICMPV6,
  166. skb->csum);
  167. skb->ip_summed = CHECKSUM_NONE;
  168. return 1;
  169. }
  170. static int tcf_csum_ipv4_tcp(struct sk_buff *skb, unsigned int ihl,
  171. unsigned int ipl)
  172. {
  173. struct tcphdr *tcph;
  174. const struct iphdr *iph;
  175. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4)
  176. return 1;
  177. tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
  178. if (tcph == NULL)
  179. return 0;
  180. iph = ip_hdr(skb);
  181. tcph->check = 0;
  182. skb->csum = csum_partial(tcph, ipl - ihl, 0);
  183. tcph->check = tcp_v4_check(ipl - ihl,
  184. iph->saddr, iph->daddr, skb->csum);
  185. skb->ip_summed = CHECKSUM_NONE;
  186. return 1;
  187. }
  188. static int tcf_csum_ipv6_tcp(struct sk_buff *skb, unsigned int ihl,
  189. unsigned int ipl)
  190. {
  191. struct tcphdr *tcph;
  192. const struct ipv6hdr *ip6h;
  193. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6)
  194. return 1;
  195. tcph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*tcph));
  196. if (tcph == NULL)
  197. return 0;
  198. ip6h = ipv6_hdr(skb);
  199. tcph->check = 0;
  200. skb->csum = csum_partial(tcph, ipl - ihl, 0);
  201. tcph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr,
  202. ipl - ihl, IPPROTO_TCP,
  203. skb->csum);
  204. skb->ip_summed = CHECKSUM_NONE;
  205. return 1;
  206. }
  207. static int tcf_csum_ipv4_udp(struct sk_buff *skb, unsigned int ihl,
  208. unsigned int ipl, int udplite)
  209. {
  210. struct udphdr *udph;
  211. const struct iphdr *iph;
  212. u16 ul;
  213. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  214. return 1;
  215. /*
  216. * Support both UDP and UDPLITE checksum algorithms, Don't use
  217. * udph->len to get the real length without any protocol check,
  218. * UDPLITE uses udph->len for another thing,
  219. * Use iph->tot_len, or just ipl.
  220. */
  221. udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
  222. if (udph == NULL)
  223. return 0;
  224. iph = ip_hdr(skb);
  225. ul = ntohs(udph->len);
  226. if (udplite || udph->check) {
  227. udph->check = 0;
  228. if (udplite) {
  229. if (ul == 0)
  230. skb->csum = csum_partial(udph, ipl - ihl, 0);
  231. else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
  232. skb->csum = csum_partial(udph, ul, 0);
  233. else
  234. goto ignore_obscure_skb;
  235. } else {
  236. if (ul != ipl - ihl)
  237. goto ignore_obscure_skb;
  238. skb->csum = csum_partial(udph, ul, 0);
  239. }
  240. udph->check = csum_tcpudp_magic(iph->saddr, iph->daddr,
  241. ul, iph->protocol,
  242. skb->csum);
  243. if (!udph->check)
  244. udph->check = CSUM_MANGLED_0;
  245. }
  246. skb->ip_summed = CHECKSUM_NONE;
  247. ignore_obscure_skb:
  248. return 1;
  249. }
  250. static int tcf_csum_ipv6_udp(struct sk_buff *skb, unsigned int ihl,
  251. unsigned int ipl, int udplite)
  252. {
  253. struct udphdr *udph;
  254. const struct ipv6hdr *ip6h;
  255. u16 ul;
  256. if (skb_is_gso(skb) && skb_shinfo(skb)->gso_type & SKB_GSO_UDP)
  257. return 1;
  258. /*
  259. * Support both UDP and UDPLITE checksum algorithms, Don't use
  260. * udph->len to get the real length without any protocol check,
  261. * UDPLITE uses udph->len for another thing,
  262. * Use ip6h->payload_len + sizeof(*ip6h) ... , or just ipl.
  263. */
  264. udph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*udph));
  265. if (udph == NULL)
  266. return 0;
  267. ip6h = ipv6_hdr(skb);
  268. ul = ntohs(udph->len);
  269. udph->check = 0;
  270. if (udplite) {
  271. if (ul == 0)
  272. skb->csum = csum_partial(udph, ipl - ihl, 0);
  273. else if ((ul >= sizeof(*udph)) && (ul <= ipl - ihl))
  274. skb->csum = csum_partial(udph, ul, 0);
  275. else
  276. goto ignore_obscure_skb;
  277. } else {
  278. if (ul != ipl - ihl)
  279. goto ignore_obscure_skb;
  280. skb->csum = csum_partial(udph, ul, 0);
  281. }
  282. udph->check = csum_ipv6_magic(&ip6h->saddr, &ip6h->daddr, ul,
  283. udplite ? IPPROTO_UDPLITE : IPPROTO_UDP,
  284. skb->csum);
  285. if (!udph->check)
  286. udph->check = CSUM_MANGLED_0;
  287. skb->ip_summed = CHECKSUM_NONE;
  288. ignore_obscure_skb:
  289. return 1;
  290. }
  291. static int tcf_csum_sctp(struct sk_buff *skb, unsigned int ihl,
  292. unsigned int ipl)
  293. {
  294. struct sctphdr *sctph;
  295. if (skb_is_gso(skb) && skb_is_gso_sctp(skb))
  296. return 1;
  297. sctph = tcf_csum_skb_nextlayer(skb, ihl, ipl, sizeof(*sctph));
  298. if (!sctph)
  299. return 0;
  300. sctph->checksum = sctp_compute_cksum(skb,
  301. skb_network_offset(skb) + ihl);
  302. skb_reset_csum_not_inet(skb);
  303. return 1;
  304. }
  305. static int tcf_csum_ipv4(struct sk_buff *skb, u32 update_flags)
  306. {
  307. const struct iphdr *iph;
  308. int ntkoff;
  309. ntkoff = skb_network_offset(skb);
  310. if (!pskb_may_pull(skb, sizeof(*iph) + ntkoff))
  311. goto fail;
  312. iph = ip_hdr(skb);
  313. switch (iph->frag_off & htons(IP_OFFSET) ? 0 : iph->protocol) {
  314. case IPPROTO_ICMP:
  315. if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
  316. if (!tcf_csum_ipv4_icmp(skb, iph->ihl * 4,
  317. ntohs(iph->tot_len)))
  318. goto fail;
  319. break;
  320. case IPPROTO_IGMP:
  321. if (update_flags & TCA_CSUM_UPDATE_FLAG_IGMP)
  322. if (!tcf_csum_ipv4_igmp(skb, iph->ihl * 4,
  323. ntohs(iph->tot_len)))
  324. goto fail;
  325. break;
  326. case IPPROTO_TCP:
  327. if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
  328. if (!tcf_csum_ipv4_tcp(skb, iph->ihl * 4,
  329. ntohs(iph->tot_len)))
  330. goto fail;
  331. break;
  332. case IPPROTO_UDP:
  333. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
  334. if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
  335. ntohs(iph->tot_len), 0))
  336. goto fail;
  337. break;
  338. case IPPROTO_UDPLITE:
  339. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
  340. if (!tcf_csum_ipv4_udp(skb, iph->ihl * 4,
  341. ntohs(iph->tot_len), 1))
  342. goto fail;
  343. break;
  344. case IPPROTO_SCTP:
  345. if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
  346. !tcf_csum_sctp(skb, iph->ihl * 4, ntohs(iph->tot_len)))
  347. goto fail;
  348. break;
  349. }
  350. if (update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) {
  351. if (skb_try_make_writable(skb, sizeof(*iph) + ntkoff))
  352. goto fail;
  353. ip_send_check(ip_hdr(skb));
  354. }
  355. return 1;
  356. fail:
  357. return 0;
  358. }
  359. static int tcf_csum_ipv6_hopopts(struct ipv6_opt_hdr *ip6xh, unsigned int ixhl,
  360. unsigned int *pl)
  361. {
  362. int off, len, optlen;
  363. unsigned char *xh = (void *)ip6xh;
  364. off = sizeof(*ip6xh);
  365. len = ixhl - off;
  366. while (len > 1) {
  367. switch (xh[off]) {
  368. case IPV6_TLV_PAD1:
  369. optlen = 1;
  370. break;
  371. case IPV6_TLV_JUMBO:
  372. optlen = xh[off + 1] + 2;
  373. if (optlen != 6 || len < 6 || (off & 3) != 2)
  374. /* wrong jumbo option length/alignment */
  375. return 0;
  376. *pl = ntohl(*(__be32 *)(xh + off + 2));
  377. goto done;
  378. default:
  379. optlen = xh[off + 1] + 2;
  380. if (optlen > len)
  381. /* ignore obscure options */
  382. goto done;
  383. break;
  384. }
  385. off += optlen;
  386. len -= optlen;
  387. }
  388. done:
  389. return 1;
  390. }
  391. static int tcf_csum_ipv6(struct sk_buff *skb, u32 update_flags)
  392. {
  393. struct ipv6hdr *ip6h;
  394. struct ipv6_opt_hdr *ip6xh;
  395. unsigned int hl, ixhl;
  396. unsigned int pl;
  397. int ntkoff;
  398. u8 nexthdr;
  399. ntkoff = skb_network_offset(skb);
  400. hl = sizeof(*ip6h);
  401. if (!pskb_may_pull(skb, hl + ntkoff))
  402. goto fail;
  403. ip6h = ipv6_hdr(skb);
  404. pl = ntohs(ip6h->payload_len);
  405. nexthdr = ip6h->nexthdr;
  406. do {
  407. switch (nexthdr) {
  408. case NEXTHDR_FRAGMENT:
  409. goto ignore_skb;
  410. case NEXTHDR_ROUTING:
  411. case NEXTHDR_HOP:
  412. case NEXTHDR_DEST:
  413. if (!pskb_may_pull(skb, hl + sizeof(*ip6xh) + ntkoff))
  414. goto fail;
  415. ip6xh = (void *)(skb_network_header(skb) + hl);
  416. ixhl = ipv6_optlen(ip6xh);
  417. if (!pskb_may_pull(skb, hl + ixhl + ntkoff))
  418. goto fail;
  419. ip6xh = (void *)(skb_network_header(skb) + hl);
  420. if ((nexthdr == NEXTHDR_HOP) &&
  421. !(tcf_csum_ipv6_hopopts(ip6xh, ixhl, &pl)))
  422. goto fail;
  423. nexthdr = ip6xh->nexthdr;
  424. hl += ixhl;
  425. break;
  426. case IPPROTO_ICMPV6:
  427. if (update_flags & TCA_CSUM_UPDATE_FLAG_ICMP)
  428. if (!tcf_csum_ipv6_icmp(skb,
  429. hl, pl + sizeof(*ip6h)))
  430. goto fail;
  431. goto done;
  432. case IPPROTO_TCP:
  433. if (update_flags & TCA_CSUM_UPDATE_FLAG_TCP)
  434. if (!tcf_csum_ipv6_tcp(skb,
  435. hl, pl + sizeof(*ip6h)))
  436. goto fail;
  437. goto done;
  438. case IPPROTO_UDP:
  439. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDP)
  440. if (!tcf_csum_ipv6_udp(skb, hl,
  441. pl + sizeof(*ip6h), 0))
  442. goto fail;
  443. goto done;
  444. case IPPROTO_UDPLITE:
  445. if (update_flags & TCA_CSUM_UPDATE_FLAG_UDPLITE)
  446. if (!tcf_csum_ipv6_udp(skb, hl,
  447. pl + sizeof(*ip6h), 1))
  448. goto fail;
  449. goto done;
  450. case IPPROTO_SCTP:
  451. if ((update_flags & TCA_CSUM_UPDATE_FLAG_SCTP) &&
  452. !tcf_csum_sctp(skb, hl, pl + sizeof(*ip6h)))
  453. goto fail;
  454. goto done;
  455. default:
  456. goto ignore_skb;
  457. }
  458. } while (pskb_may_pull(skb, hl + 1 + ntkoff));
  459. done:
  460. ignore_skb:
  461. return 1;
  462. fail:
  463. return 0;
  464. }
  465. TC_INDIRECT_SCOPE int tcf_csum_act(struct sk_buff *skb,
  466. const struct tc_action *a,
  467. struct tcf_result *res)
  468. {
  469. struct tcf_csum *p = to_tcf_csum(a);
  470. bool orig_vlan_tag_present = false;
  471. unsigned int vlan_hdr_count = 0;
  472. struct tcf_csum_params *params;
  473. u32 update_flags;
  474. __be16 protocol;
  475. int action;
  476. params = rcu_dereference_bh(p->params);
  477. tcf_lastuse_update(&p->tcf_tm);
  478. tcf_action_update_bstats(&p->common, skb);
  479. action = params->action;
  480. if (unlikely(action == TC_ACT_SHOT))
  481. goto drop;
  482. update_flags = params->update_flags;
  483. protocol = skb_protocol(skb, false);
  484. again:
  485. switch (protocol) {
  486. case cpu_to_be16(ETH_P_IP):
  487. if (!tcf_csum_ipv4(skb, update_flags))
  488. goto drop;
  489. break;
  490. case cpu_to_be16(ETH_P_IPV6):
  491. if (!tcf_csum_ipv6(skb, update_flags))
  492. goto drop;
  493. break;
  494. case cpu_to_be16(ETH_P_8021AD):
  495. fallthrough;
  496. case cpu_to_be16(ETH_P_8021Q):
  497. if (skb_vlan_tag_present(skb) && !orig_vlan_tag_present) {
  498. protocol = skb->protocol;
  499. orig_vlan_tag_present = true;
  500. } else {
  501. struct vlan_hdr *vlan;
  502. if (!pskb_may_pull(skb, VLAN_HLEN))
  503. goto drop;
  504. vlan = (struct vlan_hdr *)skb->data;
  505. protocol = vlan->h_vlan_encapsulated_proto;
  506. skb_pull(skb, VLAN_HLEN);
  507. skb_reset_network_header(skb);
  508. vlan_hdr_count++;
  509. }
  510. goto again;
  511. }
  512. out:
  513. /* Restore the skb for the pulled VLAN tags */
  514. while (vlan_hdr_count--) {
  515. skb_push(skb, VLAN_HLEN);
  516. skb_reset_network_header(skb);
  517. }
  518. return action;
  519. drop:
  520. tcf_action_inc_drop_qstats(&p->common);
  521. action = TC_ACT_SHOT;
  522. goto out;
  523. }
  524. static int tcf_csum_dump(struct sk_buff *skb, struct tc_action *a, int bind,
  525. int ref)
  526. {
  527. const struct tcf_csum *p = to_tcf_csum(a);
  528. unsigned char *b = skb_tail_pointer(skb);
  529. const struct tcf_csum_params *params;
  530. struct tc_csum opt = {
  531. .index = p->tcf_index,
  532. .refcnt = refcount_read(&p->tcf_refcnt) - ref,
  533. .bindcnt = atomic_read(&p->tcf_bindcnt) - bind,
  534. };
  535. struct tcf_t t;
  536. rcu_read_lock();
  537. params = rcu_dereference(p->params);
  538. opt.action = params->action;
  539. opt.update_flags = params->update_flags;
  540. if (nla_put(skb, TCA_CSUM_PARMS, sizeof(opt), &opt))
  541. goto nla_put_failure;
  542. tcf_tm_dump(&t, &p->tcf_tm);
  543. if (nla_put_64bit(skb, TCA_CSUM_TM, sizeof(t), &t, TCA_CSUM_PAD))
  544. goto nla_put_failure;
  545. rcu_read_unlock();
  546. return skb->len;
  547. nla_put_failure:
  548. rcu_read_unlock();
  549. nlmsg_trim(skb, b);
  550. return -1;
  551. }
  552. static void tcf_csum_cleanup(struct tc_action *a)
  553. {
  554. struct tcf_csum *p = to_tcf_csum(a);
  555. struct tcf_csum_params *params;
  556. params = rcu_dereference_protected(p->params, 1);
  557. if (params)
  558. kfree_rcu(params, rcu);
  559. }
  560. static size_t tcf_csum_get_fill_size(const struct tc_action *act)
  561. {
  562. return nla_total_size(sizeof(struct tc_csum));
  563. }
  564. static int tcf_csum_offload_act_setup(struct tc_action *act, void *entry_data,
  565. u32 *index_inc, bool bind,
  566. struct netlink_ext_ack *extack)
  567. {
  568. if (bind) {
  569. struct flow_action_entry *entry = entry_data;
  570. entry->id = FLOW_ACTION_CSUM;
  571. entry->csum_flags = tcf_csum_update_flags(act);
  572. *index_inc = 1;
  573. } else {
  574. struct flow_offload_action *fl_action = entry_data;
  575. fl_action->id = FLOW_ACTION_CSUM;
  576. }
  577. return 0;
  578. }
  579. static struct tc_action_ops act_csum_ops = {
  580. .kind = "csum",
  581. .id = TCA_ID_CSUM,
  582. .owner = THIS_MODULE,
  583. .act = tcf_csum_act,
  584. .dump = tcf_csum_dump,
  585. .init = tcf_csum_init,
  586. .cleanup = tcf_csum_cleanup,
  587. .get_fill_size = tcf_csum_get_fill_size,
  588. .offload_act_setup = tcf_csum_offload_act_setup,
  589. .size = sizeof(struct tcf_csum),
  590. };
  591. MODULE_ALIAS_NET_ACT("csum");
  592. static __net_init int csum_init_net(struct net *net)
  593. {
  594. struct tc_action_net *tn = net_generic(net, act_csum_ops.net_id);
  595. return tc_action_net_init(net, tn, &act_csum_ops);
  596. }
  597. static void __net_exit csum_exit_net(struct list_head *net_list)
  598. {
  599. tc_action_net_exit(net_list, act_csum_ops.net_id);
  600. }
  601. static struct pernet_operations csum_net_ops = {
  602. .init = csum_init_net,
  603. .exit_batch = csum_exit_net,
  604. .id = &act_csum_ops.net_id,
  605. .size = sizeof(struct tc_action_net),
  606. };
  607. MODULE_DESCRIPTION("Checksum updating actions");
  608. MODULE_LICENSE("GPL");
  609. static int __init csum_init_module(void)
  610. {
  611. return tcf_register_action(&act_csum_ops, &csum_net_ops);
  612. }
  613. static void __exit csum_cleanup_module(void)
  614. {
  615. tcf_unregister_action(&act_csum_ops, &csum_net_ops);
  616. }
  617. module_init(csum_init_module);
  618. module_exit(csum_cleanup_module);