act_csum.c 17 KB

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