act_csum.c 17 KB

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