cls_flower.c 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994
  1. /*
  2. * net/sched/cls_flower.c Flower classifier
  3. *
  4. * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/rhashtable.h>
  15. #include <linux/workqueue.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/in6.h>
  18. #include <linux/ip.h>
  19. #include <linux/mpls.h>
  20. #include <net/sch_generic.h>
  21. #include <net/pkt_cls.h>
  22. #include <net/ip.h>
  23. #include <net/flow_dissector.h>
  24. #include <net/geneve.h>
  25. #include <net/dst.h>
  26. #include <net/dst_metadata.h>
  27. struct fl_flow_key {
  28. int indev_ifindex;
  29. struct flow_dissector_key_control control;
  30. struct flow_dissector_key_control enc_control;
  31. struct flow_dissector_key_basic basic;
  32. struct flow_dissector_key_eth_addrs eth;
  33. struct flow_dissector_key_vlan vlan;
  34. struct flow_dissector_key_vlan cvlan;
  35. union {
  36. struct flow_dissector_key_ipv4_addrs ipv4;
  37. struct flow_dissector_key_ipv6_addrs ipv6;
  38. };
  39. struct flow_dissector_key_ports tp;
  40. struct flow_dissector_key_icmp icmp;
  41. struct flow_dissector_key_arp arp;
  42. struct flow_dissector_key_keyid enc_key_id;
  43. union {
  44. struct flow_dissector_key_ipv4_addrs enc_ipv4;
  45. struct flow_dissector_key_ipv6_addrs enc_ipv6;
  46. };
  47. struct flow_dissector_key_ports enc_tp;
  48. struct flow_dissector_key_mpls mpls;
  49. struct flow_dissector_key_tcp tcp;
  50. struct flow_dissector_key_ip ip;
  51. struct flow_dissector_key_ip enc_ip;
  52. struct flow_dissector_key_enc_opts enc_opts;
  53. } __aligned(BITS_PER_LONG / 8); /* Ensure that we can do comparisons as longs. */
  54. struct fl_flow_mask_range {
  55. unsigned short int start;
  56. unsigned short int end;
  57. };
  58. struct fl_flow_mask {
  59. struct fl_flow_key key;
  60. struct fl_flow_mask_range range;
  61. struct rhash_head ht_node;
  62. struct rhashtable ht;
  63. struct rhashtable_params filter_ht_params;
  64. struct flow_dissector dissector;
  65. struct list_head filters;
  66. struct rcu_work rwork;
  67. struct list_head list;
  68. };
  69. struct fl_flow_tmplt {
  70. struct fl_flow_key dummy_key;
  71. struct fl_flow_key mask;
  72. struct flow_dissector dissector;
  73. struct tcf_chain *chain;
  74. };
  75. struct cls_fl_head {
  76. struct rhashtable ht;
  77. struct list_head masks;
  78. struct rcu_work rwork;
  79. struct idr handle_idr;
  80. };
  81. struct cls_fl_filter {
  82. struct fl_flow_mask *mask;
  83. struct rhash_head ht_node;
  84. struct fl_flow_key mkey;
  85. struct tcf_exts exts;
  86. struct tcf_result res;
  87. struct fl_flow_key key;
  88. struct list_head list;
  89. u32 handle;
  90. u32 flags;
  91. unsigned int in_hw_count;
  92. struct rcu_work rwork;
  93. struct net_device *hw_dev;
  94. };
  95. static const struct rhashtable_params mask_ht_params = {
  96. .key_offset = offsetof(struct fl_flow_mask, key),
  97. .key_len = sizeof(struct fl_flow_key),
  98. .head_offset = offsetof(struct fl_flow_mask, ht_node),
  99. .automatic_shrinking = true,
  100. };
  101. static unsigned short int fl_mask_range(const struct fl_flow_mask *mask)
  102. {
  103. return mask->range.end - mask->range.start;
  104. }
  105. static void fl_mask_update_range(struct fl_flow_mask *mask)
  106. {
  107. const u8 *bytes = (const u8 *) &mask->key;
  108. size_t size = sizeof(mask->key);
  109. size_t i, first = 0, last;
  110. for (i = 0; i < size; i++) {
  111. if (bytes[i]) {
  112. first = i;
  113. break;
  114. }
  115. }
  116. last = first;
  117. for (i = size - 1; i != first; i--) {
  118. if (bytes[i]) {
  119. last = i;
  120. break;
  121. }
  122. }
  123. mask->range.start = rounddown(first, sizeof(long));
  124. mask->range.end = roundup(last + 1, sizeof(long));
  125. }
  126. static void *fl_key_get_start(struct fl_flow_key *key,
  127. const struct fl_flow_mask *mask)
  128. {
  129. return (u8 *) key + mask->range.start;
  130. }
  131. static void fl_set_masked_key(struct fl_flow_key *mkey, struct fl_flow_key *key,
  132. struct fl_flow_mask *mask)
  133. {
  134. const long *lkey = fl_key_get_start(key, mask);
  135. const long *lmask = fl_key_get_start(&mask->key, mask);
  136. long *lmkey = fl_key_get_start(mkey, mask);
  137. int i;
  138. for (i = 0; i < fl_mask_range(mask); i += sizeof(long))
  139. *lmkey++ = *lkey++ & *lmask++;
  140. }
  141. static bool fl_mask_fits_tmplt(struct fl_flow_tmplt *tmplt,
  142. struct fl_flow_mask *mask)
  143. {
  144. const long *lmask = fl_key_get_start(&mask->key, mask);
  145. const long *ltmplt;
  146. int i;
  147. if (!tmplt)
  148. return true;
  149. ltmplt = fl_key_get_start(&tmplt->mask, mask);
  150. for (i = 0; i < fl_mask_range(mask); i += sizeof(long)) {
  151. if (~*ltmplt++ & *lmask++)
  152. return false;
  153. }
  154. return true;
  155. }
  156. static void fl_clear_masked_range(struct fl_flow_key *key,
  157. struct fl_flow_mask *mask)
  158. {
  159. memset(fl_key_get_start(key, mask), 0, fl_mask_range(mask));
  160. }
  161. static struct cls_fl_filter *fl_lookup(struct fl_flow_mask *mask,
  162. struct fl_flow_key *mkey)
  163. {
  164. return rhashtable_lookup_fast(&mask->ht, fl_key_get_start(mkey, mask),
  165. mask->filter_ht_params);
  166. }
  167. static int fl_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  168. struct tcf_result *res)
  169. {
  170. struct cls_fl_head *head = rcu_dereference_bh(tp->root);
  171. struct cls_fl_filter *f;
  172. struct fl_flow_mask *mask;
  173. struct fl_flow_key skb_key;
  174. struct fl_flow_key skb_mkey;
  175. list_for_each_entry_rcu(mask, &head->masks, list) {
  176. flow_dissector_init_keys(&skb_key.control, &skb_key.basic);
  177. fl_clear_masked_range(&skb_key, mask);
  178. skb_key.indev_ifindex = skb->skb_iif;
  179. /* skb_flow_dissect() does not set n_proto in case an unknown
  180. * protocol, so do it rather here.
  181. */
  182. skb_key.basic.n_proto = skb->protocol;
  183. skb_flow_dissect_tunnel_info(skb, &mask->dissector, &skb_key);
  184. skb_flow_dissect(skb, &mask->dissector, &skb_key, 0);
  185. fl_set_masked_key(&skb_mkey, &skb_key, mask);
  186. f = fl_lookup(mask, &skb_mkey);
  187. if (f && !tc_skip_sw(f->flags)) {
  188. *res = f->res;
  189. return tcf_exts_exec(skb, &f->exts, res);
  190. }
  191. }
  192. return -1;
  193. }
  194. static int fl_init(struct tcf_proto *tp)
  195. {
  196. struct cls_fl_head *head;
  197. head = kzalloc(sizeof(*head), GFP_KERNEL);
  198. if (!head)
  199. return -ENOBUFS;
  200. INIT_LIST_HEAD_RCU(&head->masks);
  201. rcu_assign_pointer(tp->root, head);
  202. idr_init(&head->handle_idr);
  203. return rhashtable_init(&head->ht, &mask_ht_params);
  204. }
  205. static void fl_mask_free(struct fl_flow_mask *mask)
  206. {
  207. rhashtable_destroy(&mask->ht);
  208. kfree(mask);
  209. }
  210. static void fl_mask_free_work(struct work_struct *work)
  211. {
  212. struct fl_flow_mask *mask = container_of(to_rcu_work(work),
  213. struct fl_flow_mask, rwork);
  214. fl_mask_free(mask);
  215. }
  216. static bool fl_mask_put(struct cls_fl_head *head, struct fl_flow_mask *mask,
  217. bool async)
  218. {
  219. if (!list_empty(&mask->filters))
  220. return false;
  221. rhashtable_remove_fast(&head->ht, &mask->ht_node, mask_ht_params);
  222. list_del_rcu(&mask->list);
  223. if (async)
  224. tcf_queue_work(&mask->rwork, fl_mask_free_work);
  225. else
  226. fl_mask_free(mask);
  227. return true;
  228. }
  229. static void __fl_destroy_filter(struct cls_fl_filter *f)
  230. {
  231. tcf_exts_destroy(&f->exts);
  232. tcf_exts_put_net(&f->exts);
  233. kfree(f);
  234. }
  235. static void fl_destroy_filter_work(struct work_struct *work)
  236. {
  237. struct cls_fl_filter *f = container_of(to_rcu_work(work),
  238. struct cls_fl_filter, rwork);
  239. rtnl_lock();
  240. __fl_destroy_filter(f);
  241. rtnl_unlock();
  242. }
  243. static void fl_hw_destroy_filter(struct tcf_proto *tp, struct cls_fl_filter *f,
  244. struct netlink_ext_ack *extack)
  245. {
  246. struct tc_cls_flower_offload cls_flower = {};
  247. struct tcf_block *block = tp->chain->block;
  248. tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
  249. cls_flower.command = TC_CLSFLOWER_DESTROY;
  250. cls_flower.cookie = (unsigned long) f;
  251. tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
  252. &cls_flower, false);
  253. tcf_block_offload_dec(block, &f->flags);
  254. }
  255. static int fl_hw_replace_filter(struct tcf_proto *tp,
  256. struct cls_fl_filter *f,
  257. struct netlink_ext_ack *extack)
  258. {
  259. struct tc_cls_flower_offload cls_flower = {};
  260. struct tcf_block *block = tp->chain->block;
  261. bool skip_sw = tc_skip_sw(f->flags);
  262. int err;
  263. tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, extack);
  264. cls_flower.command = TC_CLSFLOWER_REPLACE;
  265. cls_flower.cookie = (unsigned long) f;
  266. cls_flower.dissector = &f->mask->dissector;
  267. cls_flower.mask = &f->mask->key;
  268. cls_flower.key = &f->mkey;
  269. cls_flower.exts = &f->exts;
  270. cls_flower.classid = f->res.classid;
  271. err = tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
  272. &cls_flower, skip_sw);
  273. if (err < 0) {
  274. fl_hw_destroy_filter(tp, f, NULL);
  275. return err;
  276. } else if (err > 0) {
  277. f->in_hw_count = err;
  278. tcf_block_offload_inc(block, &f->flags);
  279. }
  280. if (skip_sw && !(f->flags & TCA_CLS_FLAGS_IN_HW))
  281. return -EINVAL;
  282. return 0;
  283. }
  284. static void fl_hw_update_stats(struct tcf_proto *tp, struct cls_fl_filter *f)
  285. {
  286. struct tc_cls_flower_offload cls_flower = {};
  287. struct tcf_block *block = tp->chain->block;
  288. tc_cls_common_offload_init(&cls_flower.common, tp, f->flags, NULL);
  289. cls_flower.command = TC_CLSFLOWER_STATS;
  290. cls_flower.cookie = (unsigned long) f;
  291. cls_flower.exts = &f->exts;
  292. cls_flower.classid = f->res.classid;
  293. tc_setup_cb_call(block, &f->exts, TC_SETUP_CLSFLOWER,
  294. &cls_flower, false);
  295. }
  296. static bool __fl_delete(struct tcf_proto *tp, struct cls_fl_filter *f,
  297. struct netlink_ext_ack *extack)
  298. {
  299. struct cls_fl_head *head = rtnl_dereference(tp->root);
  300. bool async = tcf_exts_get_net(&f->exts);
  301. bool last;
  302. idr_remove(&head->handle_idr, f->handle);
  303. list_del_rcu(&f->list);
  304. last = fl_mask_put(head, f->mask, async);
  305. if (!tc_skip_hw(f->flags))
  306. fl_hw_destroy_filter(tp, f, extack);
  307. tcf_unbind_filter(tp, &f->res);
  308. if (async)
  309. tcf_queue_work(&f->rwork, fl_destroy_filter_work);
  310. else
  311. __fl_destroy_filter(f);
  312. return last;
  313. }
  314. static void fl_destroy_sleepable(struct work_struct *work)
  315. {
  316. struct cls_fl_head *head = container_of(to_rcu_work(work),
  317. struct cls_fl_head,
  318. rwork);
  319. rhashtable_destroy(&head->ht);
  320. kfree(head);
  321. module_put(THIS_MODULE);
  322. }
  323. static void fl_destroy(struct tcf_proto *tp, struct netlink_ext_ack *extack)
  324. {
  325. struct cls_fl_head *head = rtnl_dereference(tp->root);
  326. struct fl_flow_mask *mask, *next_mask;
  327. struct cls_fl_filter *f, *next;
  328. list_for_each_entry_safe(mask, next_mask, &head->masks, list) {
  329. list_for_each_entry_safe(f, next, &mask->filters, list) {
  330. if (__fl_delete(tp, f, extack))
  331. break;
  332. }
  333. }
  334. idr_destroy(&head->handle_idr);
  335. __module_get(THIS_MODULE);
  336. tcf_queue_work(&head->rwork, fl_destroy_sleepable);
  337. }
  338. static void *fl_get(struct tcf_proto *tp, u32 handle)
  339. {
  340. struct cls_fl_head *head = rtnl_dereference(tp->root);
  341. return idr_find(&head->handle_idr, handle);
  342. }
  343. static const struct nla_policy fl_policy[TCA_FLOWER_MAX + 1] = {
  344. [TCA_FLOWER_UNSPEC] = { .type = NLA_UNSPEC },
  345. [TCA_FLOWER_CLASSID] = { .type = NLA_U32 },
  346. [TCA_FLOWER_INDEV] = { .type = NLA_STRING,
  347. .len = IFNAMSIZ },
  348. [TCA_FLOWER_KEY_ETH_DST] = { .len = ETH_ALEN },
  349. [TCA_FLOWER_KEY_ETH_DST_MASK] = { .len = ETH_ALEN },
  350. [TCA_FLOWER_KEY_ETH_SRC] = { .len = ETH_ALEN },
  351. [TCA_FLOWER_KEY_ETH_SRC_MASK] = { .len = ETH_ALEN },
  352. [TCA_FLOWER_KEY_ETH_TYPE] = { .type = NLA_U16 },
  353. [TCA_FLOWER_KEY_IP_PROTO] = { .type = NLA_U8 },
  354. [TCA_FLOWER_KEY_IPV4_SRC] = { .type = NLA_U32 },
  355. [TCA_FLOWER_KEY_IPV4_SRC_MASK] = { .type = NLA_U32 },
  356. [TCA_FLOWER_KEY_IPV4_DST] = { .type = NLA_U32 },
  357. [TCA_FLOWER_KEY_IPV4_DST_MASK] = { .type = NLA_U32 },
  358. [TCA_FLOWER_KEY_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  359. [TCA_FLOWER_KEY_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
  360. [TCA_FLOWER_KEY_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  361. [TCA_FLOWER_KEY_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
  362. [TCA_FLOWER_KEY_TCP_SRC] = { .type = NLA_U16 },
  363. [TCA_FLOWER_KEY_TCP_DST] = { .type = NLA_U16 },
  364. [TCA_FLOWER_KEY_UDP_SRC] = { .type = NLA_U16 },
  365. [TCA_FLOWER_KEY_UDP_DST] = { .type = NLA_U16 },
  366. [TCA_FLOWER_KEY_VLAN_ID] = { .type = NLA_U16 },
  367. [TCA_FLOWER_KEY_VLAN_PRIO] = { .type = NLA_U8 },
  368. [TCA_FLOWER_KEY_VLAN_ETH_TYPE] = { .type = NLA_U16 },
  369. [TCA_FLOWER_KEY_ENC_KEY_ID] = { .type = NLA_U32 },
  370. [TCA_FLOWER_KEY_ENC_IPV4_SRC] = { .type = NLA_U32 },
  371. [TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK] = { .type = NLA_U32 },
  372. [TCA_FLOWER_KEY_ENC_IPV4_DST] = { .type = NLA_U32 },
  373. [TCA_FLOWER_KEY_ENC_IPV4_DST_MASK] = { .type = NLA_U32 },
  374. [TCA_FLOWER_KEY_ENC_IPV6_SRC] = { .len = sizeof(struct in6_addr) },
  375. [TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK] = { .len = sizeof(struct in6_addr) },
  376. [TCA_FLOWER_KEY_ENC_IPV6_DST] = { .len = sizeof(struct in6_addr) },
  377. [TCA_FLOWER_KEY_ENC_IPV6_DST_MASK] = { .len = sizeof(struct in6_addr) },
  378. [TCA_FLOWER_KEY_TCP_SRC_MASK] = { .type = NLA_U16 },
  379. [TCA_FLOWER_KEY_TCP_DST_MASK] = { .type = NLA_U16 },
  380. [TCA_FLOWER_KEY_UDP_SRC_MASK] = { .type = NLA_U16 },
  381. [TCA_FLOWER_KEY_UDP_DST_MASK] = { .type = NLA_U16 },
  382. [TCA_FLOWER_KEY_SCTP_SRC_MASK] = { .type = NLA_U16 },
  383. [TCA_FLOWER_KEY_SCTP_DST_MASK] = { .type = NLA_U16 },
  384. [TCA_FLOWER_KEY_SCTP_SRC] = { .type = NLA_U16 },
  385. [TCA_FLOWER_KEY_SCTP_DST] = { .type = NLA_U16 },
  386. [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT] = { .type = NLA_U16 },
  387. [TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK] = { .type = NLA_U16 },
  388. [TCA_FLOWER_KEY_ENC_UDP_DST_PORT] = { .type = NLA_U16 },
  389. [TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK] = { .type = NLA_U16 },
  390. [TCA_FLOWER_KEY_FLAGS] = { .type = NLA_U32 },
  391. [TCA_FLOWER_KEY_FLAGS_MASK] = { .type = NLA_U32 },
  392. [TCA_FLOWER_KEY_ICMPV4_TYPE] = { .type = NLA_U8 },
  393. [TCA_FLOWER_KEY_ICMPV4_TYPE_MASK] = { .type = NLA_U8 },
  394. [TCA_FLOWER_KEY_ICMPV4_CODE] = { .type = NLA_U8 },
  395. [TCA_FLOWER_KEY_ICMPV4_CODE_MASK] = { .type = NLA_U8 },
  396. [TCA_FLOWER_KEY_ICMPV6_TYPE] = { .type = NLA_U8 },
  397. [TCA_FLOWER_KEY_ICMPV6_TYPE_MASK] = { .type = NLA_U8 },
  398. [TCA_FLOWER_KEY_ICMPV6_CODE] = { .type = NLA_U8 },
  399. [TCA_FLOWER_KEY_ICMPV6_CODE_MASK] = { .type = NLA_U8 },
  400. [TCA_FLOWER_KEY_ARP_SIP] = { .type = NLA_U32 },
  401. [TCA_FLOWER_KEY_ARP_SIP_MASK] = { .type = NLA_U32 },
  402. [TCA_FLOWER_KEY_ARP_TIP] = { .type = NLA_U32 },
  403. [TCA_FLOWER_KEY_ARP_TIP_MASK] = { .type = NLA_U32 },
  404. [TCA_FLOWER_KEY_ARP_OP] = { .type = NLA_U8 },
  405. [TCA_FLOWER_KEY_ARP_OP_MASK] = { .type = NLA_U8 },
  406. [TCA_FLOWER_KEY_ARP_SHA] = { .len = ETH_ALEN },
  407. [TCA_FLOWER_KEY_ARP_SHA_MASK] = { .len = ETH_ALEN },
  408. [TCA_FLOWER_KEY_ARP_THA] = { .len = ETH_ALEN },
  409. [TCA_FLOWER_KEY_ARP_THA_MASK] = { .len = ETH_ALEN },
  410. [TCA_FLOWER_KEY_MPLS_TTL] = { .type = NLA_U8 },
  411. [TCA_FLOWER_KEY_MPLS_BOS] = { .type = NLA_U8 },
  412. [TCA_FLOWER_KEY_MPLS_TC] = { .type = NLA_U8 },
  413. [TCA_FLOWER_KEY_MPLS_LABEL] = { .type = NLA_U32 },
  414. [TCA_FLOWER_KEY_TCP_FLAGS] = { .type = NLA_U16 },
  415. [TCA_FLOWER_KEY_TCP_FLAGS_MASK] = { .type = NLA_U16 },
  416. [TCA_FLOWER_KEY_IP_TOS] = { .type = NLA_U8 },
  417. [TCA_FLOWER_KEY_IP_TOS_MASK] = { .type = NLA_U8 },
  418. [TCA_FLOWER_KEY_IP_TTL] = { .type = NLA_U8 },
  419. [TCA_FLOWER_KEY_IP_TTL_MASK] = { .type = NLA_U8 },
  420. [TCA_FLOWER_KEY_CVLAN_ID] = { .type = NLA_U16 },
  421. [TCA_FLOWER_KEY_CVLAN_PRIO] = { .type = NLA_U8 },
  422. [TCA_FLOWER_KEY_CVLAN_ETH_TYPE] = { .type = NLA_U16 },
  423. [TCA_FLOWER_KEY_ENC_IP_TOS] = { .type = NLA_U8 },
  424. [TCA_FLOWER_KEY_ENC_IP_TOS_MASK] = { .type = NLA_U8 },
  425. [TCA_FLOWER_KEY_ENC_IP_TTL] = { .type = NLA_U8 },
  426. [TCA_FLOWER_KEY_ENC_IP_TTL_MASK] = { .type = NLA_U8 },
  427. [TCA_FLOWER_KEY_ENC_OPTS] = { .type = NLA_NESTED },
  428. [TCA_FLOWER_KEY_ENC_OPTS_MASK] = { .type = NLA_NESTED },
  429. [TCA_FLOWER_FLAGS] = { .type = NLA_U32 },
  430. };
  431. static const struct nla_policy
  432. enc_opts_policy[TCA_FLOWER_KEY_ENC_OPTS_MAX + 1] = {
  433. [TCA_FLOWER_KEY_ENC_OPTS_GENEVE] = { .type = NLA_NESTED },
  434. };
  435. static const struct nla_policy
  436. geneve_opt_policy[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1] = {
  437. [TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] = { .type = NLA_U16 },
  438. [TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] = { .type = NLA_U8 },
  439. [TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA] = { .type = NLA_BINARY,
  440. .len = 128 },
  441. };
  442. static void fl_set_key_val(struct nlattr **tb,
  443. void *val, int val_type,
  444. void *mask, int mask_type, int len)
  445. {
  446. if (!tb[val_type])
  447. return;
  448. memcpy(val, nla_data(tb[val_type]), len);
  449. if (mask_type == TCA_FLOWER_UNSPEC || !tb[mask_type])
  450. memset(mask, 0xff, len);
  451. else
  452. memcpy(mask, nla_data(tb[mask_type]), len);
  453. }
  454. static int fl_set_key_mpls(struct nlattr **tb,
  455. struct flow_dissector_key_mpls *key_val,
  456. struct flow_dissector_key_mpls *key_mask)
  457. {
  458. if (tb[TCA_FLOWER_KEY_MPLS_TTL]) {
  459. key_val->mpls_ttl = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TTL]);
  460. key_mask->mpls_ttl = MPLS_TTL_MASK;
  461. }
  462. if (tb[TCA_FLOWER_KEY_MPLS_BOS]) {
  463. u8 bos = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_BOS]);
  464. if (bos & ~MPLS_BOS_MASK)
  465. return -EINVAL;
  466. key_val->mpls_bos = bos;
  467. key_mask->mpls_bos = MPLS_BOS_MASK;
  468. }
  469. if (tb[TCA_FLOWER_KEY_MPLS_TC]) {
  470. u8 tc = nla_get_u8(tb[TCA_FLOWER_KEY_MPLS_TC]);
  471. if (tc & ~MPLS_TC_MASK)
  472. return -EINVAL;
  473. key_val->mpls_tc = tc;
  474. key_mask->mpls_tc = MPLS_TC_MASK;
  475. }
  476. if (tb[TCA_FLOWER_KEY_MPLS_LABEL]) {
  477. u32 label = nla_get_u32(tb[TCA_FLOWER_KEY_MPLS_LABEL]);
  478. if (label & ~MPLS_LABEL_MASK)
  479. return -EINVAL;
  480. key_val->mpls_label = label;
  481. key_mask->mpls_label = MPLS_LABEL_MASK;
  482. }
  483. return 0;
  484. }
  485. static void fl_set_key_vlan(struct nlattr **tb,
  486. __be16 ethertype,
  487. int vlan_id_key, int vlan_prio_key,
  488. struct flow_dissector_key_vlan *key_val,
  489. struct flow_dissector_key_vlan *key_mask)
  490. {
  491. #define VLAN_PRIORITY_MASK 0x7
  492. if (tb[vlan_id_key]) {
  493. key_val->vlan_id =
  494. nla_get_u16(tb[vlan_id_key]) & VLAN_VID_MASK;
  495. key_mask->vlan_id = VLAN_VID_MASK;
  496. }
  497. if (tb[vlan_prio_key]) {
  498. key_val->vlan_priority =
  499. nla_get_u8(tb[vlan_prio_key]) &
  500. VLAN_PRIORITY_MASK;
  501. key_mask->vlan_priority = VLAN_PRIORITY_MASK;
  502. }
  503. key_val->vlan_tpid = ethertype;
  504. key_mask->vlan_tpid = cpu_to_be16(~0);
  505. }
  506. static void fl_set_key_flag(u32 flower_key, u32 flower_mask,
  507. u32 *dissector_key, u32 *dissector_mask,
  508. u32 flower_flag_bit, u32 dissector_flag_bit)
  509. {
  510. if (flower_mask & flower_flag_bit) {
  511. *dissector_mask |= dissector_flag_bit;
  512. if (flower_key & flower_flag_bit)
  513. *dissector_key |= dissector_flag_bit;
  514. }
  515. }
  516. static int fl_set_key_flags(struct nlattr **tb,
  517. u32 *flags_key, u32 *flags_mask)
  518. {
  519. u32 key, mask;
  520. /* mask is mandatory for flags */
  521. if (!tb[TCA_FLOWER_KEY_FLAGS_MASK])
  522. return -EINVAL;
  523. key = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS]));
  524. mask = be32_to_cpu(nla_get_u32(tb[TCA_FLOWER_KEY_FLAGS_MASK]));
  525. *flags_key = 0;
  526. *flags_mask = 0;
  527. fl_set_key_flag(key, mask, flags_key, flags_mask,
  528. TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
  529. fl_set_key_flag(key, mask, flags_key, flags_mask,
  530. TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
  531. FLOW_DIS_FIRST_FRAG);
  532. return 0;
  533. }
  534. static void fl_set_key_ip(struct nlattr **tb, bool encap,
  535. struct flow_dissector_key_ip *key,
  536. struct flow_dissector_key_ip *mask)
  537. {
  538. int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
  539. int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
  540. int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
  541. int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
  542. fl_set_key_val(tb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos));
  543. fl_set_key_val(tb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl));
  544. }
  545. static int fl_set_geneve_opt(const struct nlattr *nla, struct fl_flow_key *key,
  546. int depth, int option_len,
  547. struct netlink_ext_ack *extack)
  548. {
  549. struct nlattr *tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX + 1];
  550. struct nlattr *class = NULL, *type = NULL, *data = NULL;
  551. struct geneve_opt *opt;
  552. int err, data_len = 0;
  553. if (option_len > sizeof(struct geneve_opt))
  554. data_len = option_len - sizeof(struct geneve_opt);
  555. opt = (struct geneve_opt *)&key->enc_opts.data[key->enc_opts.len];
  556. memset(opt, 0xff, option_len);
  557. opt->length = data_len / 4;
  558. opt->r1 = 0;
  559. opt->r2 = 0;
  560. opt->r3 = 0;
  561. /* If no mask has been prodived we assume an exact match. */
  562. if (!depth)
  563. return sizeof(struct geneve_opt) + data_len;
  564. if (nla_type(nla) != TCA_FLOWER_KEY_ENC_OPTS_GENEVE) {
  565. NL_SET_ERR_MSG(extack, "Non-geneve option type for mask");
  566. return -EINVAL;
  567. }
  568. err = nla_parse_nested(tb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_MAX,
  569. nla, geneve_opt_policy, extack);
  570. if (err < 0)
  571. return err;
  572. /* We are not allowed to omit any of CLASS, TYPE or DATA
  573. * fields from the key.
  574. */
  575. if (!option_len &&
  576. (!tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS] ||
  577. !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE] ||
  578. !tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA])) {
  579. NL_SET_ERR_MSG(extack, "Missing tunnel key geneve option class, type or data");
  580. return -EINVAL;
  581. }
  582. /* Omitting any of CLASS, TYPE or DATA fields is allowed
  583. * for the mask.
  584. */
  585. if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA]) {
  586. int new_len = key->enc_opts.len;
  587. data = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA];
  588. data_len = nla_len(data);
  589. if (data_len < 4) {
  590. NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is less than 4 bytes long");
  591. return -ERANGE;
  592. }
  593. if (data_len % 4) {
  594. NL_SET_ERR_MSG(extack, "Tunnel key geneve option data is not a multiple of 4 bytes long");
  595. return -ERANGE;
  596. }
  597. new_len += sizeof(struct geneve_opt) + data_len;
  598. BUILD_BUG_ON(FLOW_DIS_TUN_OPTS_MAX != IP_TUNNEL_OPTS_MAX);
  599. if (new_len > FLOW_DIS_TUN_OPTS_MAX) {
  600. NL_SET_ERR_MSG(extack, "Tunnel options exceeds max size");
  601. return -ERANGE;
  602. }
  603. opt->length = data_len / 4;
  604. memcpy(opt->opt_data, nla_data(data), data_len);
  605. }
  606. if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS]) {
  607. class = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS];
  608. opt->opt_class = nla_get_be16(class);
  609. }
  610. if (tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE]) {
  611. type = tb[TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE];
  612. opt->type = nla_get_u8(type);
  613. }
  614. return sizeof(struct geneve_opt) + data_len;
  615. }
  616. static int fl_set_enc_opt(struct nlattr **tb, struct fl_flow_key *key,
  617. struct fl_flow_key *mask,
  618. struct netlink_ext_ack *extack)
  619. {
  620. const struct nlattr *nla_enc_key, *nla_opt_key, *nla_opt_msk = NULL;
  621. int err, option_len, key_depth, msk_depth = 0;
  622. err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS],
  623. TCA_FLOWER_KEY_ENC_OPTS_MAX,
  624. enc_opts_policy, extack);
  625. if (err)
  626. return err;
  627. nla_enc_key = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS]);
  628. if (tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]) {
  629. err = nla_validate_nested(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK],
  630. TCA_FLOWER_KEY_ENC_OPTS_MAX,
  631. enc_opts_policy, extack);
  632. if (err)
  633. return err;
  634. nla_opt_msk = nla_data(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
  635. msk_depth = nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS_MASK]);
  636. }
  637. nla_for_each_attr(nla_opt_key, nla_enc_key,
  638. nla_len(tb[TCA_FLOWER_KEY_ENC_OPTS]), key_depth) {
  639. switch (nla_type(nla_opt_key)) {
  640. case TCA_FLOWER_KEY_ENC_OPTS_GENEVE:
  641. option_len = 0;
  642. key->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
  643. option_len = fl_set_geneve_opt(nla_opt_key, key,
  644. key_depth, option_len,
  645. extack);
  646. if (option_len < 0)
  647. return option_len;
  648. key->enc_opts.len += option_len;
  649. /* At the same time we need to parse through the mask
  650. * in order to verify exact and mask attribute lengths.
  651. */
  652. mask->enc_opts.dst_opt_type = TUNNEL_GENEVE_OPT;
  653. option_len = fl_set_geneve_opt(nla_opt_msk, mask,
  654. msk_depth, option_len,
  655. extack);
  656. if (option_len < 0)
  657. return option_len;
  658. mask->enc_opts.len += option_len;
  659. if (key->enc_opts.len != mask->enc_opts.len) {
  660. NL_SET_ERR_MSG(extack, "Key and mask miss aligned");
  661. return -EINVAL;
  662. }
  663. if (msk_depth)
  664. nla_opt_msk = nla_next(nla_opt_msk, &msk_depth);
  665. break;
  666. default:
  667. NL_SET_ERR_MSG(extack, "Unknown tunnel option type");
  668. return -EINVAL;
  669. }
  670. }
  671. return 0;
  672. }
  673. static int fl_set_key(struct net *net, struct nlattr **tb,
  674. struct fl_flow_key *key, struct fl_flow_key *mask,
  675. struct netlink_ext_ack *extack)
  676. {
  677. __be16 ethertype;
  678. int ret = 0;
  679. #ifdef CONFIG_NET_CLS_IND
  680. if (tb[TCA_FLOWER_INDEV]) {
  681. int err = tcf_change_indev(net, tb[TCA_FLOWER_INDEV], extack);
  682. if (err < 0)
  683. return err;
  684. key->indev_ifindex = err;
  685. mask->indev_ifindex = 0xffffffff;
  686. }
  687. #endif
  688. fl_set_key_val(tb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
  689. mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
  690. sizeof(key->eth.dst));
  691. fl_set_key_val(tb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
  692. mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
  693. sizeof(key->eth.src));
  694. if (tb[TCA_FLOWER_KEY_ETH_TYPE]) {
  695. ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_ETH_TYPE]);
  696. if (eth_type_vlan(ethertype)) {
  697. fl_set_key_vlan(tb, ethertype, TCA_FLOWER_KEY_VLAN_ID,
  698. TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan,
  699. &mask->vlan);
  700. if (tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]) {
  701. ethertype = nla_get_be16(tb[TCA_FLOWER_KEY_VLAN_ETH_TYPE]);
  702. if (eth_type_vlan(ethertype)) {
  703. fl_set_key_vlan(tb, ethertype,
  704. TCA_FLOWER_KEY_CVLAN_ID,
  705. TCA_FLOWER_KEY_CVLAN_PRIO,
  706. &key->cvlan, &mask->cvlan);
  707. fl_set_key_val(tb, &key->basic.n_proto,
  708. TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
  709. &mask->basic.n_proto,
  710. TCA_FLOWER_UNSPEC,
  711. sizeof(key->basic.n_proto));
  712. } else {
  713. key->basic.n_proto = ethertype;
  714. mask->basic.n_proto = cpu_to_be16(~0);
  715. }
  716. }
  717. } else {
  718. key->basic.n_proto = ethertype;
  719. mask->basic.n_proto = cpu_to_be16(~0);
  720. }
  721. }
  722. if (key->basic.n_proto == htons(ETH_P_IP) ||
  723. key->basic.n_proto == htons(ETH_P_IPV6)) {
  724. fl_set_key_val(tb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
  725. &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
  726. sizeof(key->basic.ip_proto));
  727. fl_set_key_ip(tb, false, &key->ip, &mask->ip);
  728. }
  729. if (tb[TCA_FLOWER_KEY_IPV4_SRC] || tb[TCA_FLOWER_KEY_IPV4_DST]) {
  730. key->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  731. mask->control.addr_type = ~0;
  732. fl_set_key_val(tb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
  733. &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
  734. sizeof(key->ipv4.src));
  735. fl_set_key_val(tb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
  736. &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
  737. sizeof(key->ipv4.dst));
  738. } else if (tb[TCA_FLOWER_KEY_IPV6_SRC] || tb[TCA_FLOWER_KEY_IPV6_DST]) {
  739. key->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  740. mask->control.addr_type = ~0;
  741. fl_set_key_val(tb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
  742. &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
  743. sizeof(key->ipv6.src));
  744. fl_set_key_val(tb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
  745. &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
  746. sizeof(key->ipv6.dst));
  747. }
  748. if (key->basic.ip_proto == IPPROTO_TCP) {
  749. fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
  750. &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
  751. sizeof(key->tp.src));
  752. fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
  753. &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
  754. sizeof(key->tp.dst));
  755. fl_set_key_val(tb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
  756. &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
  757. sizeof(key->tcp.flags));
  758. } else if (key->basic.ip_proto == IPPROTO_UDP) {
  759. fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
  760. &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
  761. sizeof(key->tp.src));
  762. fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
  763. &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
  764. sizeof(key->tp.dst));
  765. } else if (key->basic.ip_proto == IPPROTO_SCTP) {
  766. fl_set_key_val(tb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
  767. &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
  768. sizeof(key->tp.src));
  769. fl_set_key_val(tb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
  770. &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
  771. sizeof(key->tp.dst));
  772. } else if (key->basic.n_proto == htons(ETH_P_IP) &&
  773. key->basic.ip_proto == IPPROTO_ICMP) {
  774. fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV4_TYPE,
  775. &mask->icmp.type,
  776. TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
  777. sizeof(key->icmp.type));
  778. fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV4_CODE,
  779. &mask->icmp.code,
  780. TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
  781. sizeof(key->icmp.code));
  782. } else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
  783. key->basic.ip_proto == IPPROTO_ICMPV6) {
  784. fl_set_key_val(tb, &key->icmp.type, TCA_FLOWER_KEY_ICMPV6_TYPE,
  785. &mask->icmp.type,
  786. TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
  787. sizeof(key->icmp.type));
  788. fl_set_key_val(tb, &key->icmp.code, TCA_FLOWER_KEY_ICMPV6_CODE,
  789. &mask->icmp.code,
  790. TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
  791. sizeof(key->icmp.code));
  792. } else if (key->basic.n_proto == htons(ETH_P_MPLS_UC) ||
  793. key->basic.n_proto == htons(ETH_P_MPLS_MC)) {
  794. ret = fl_set_key_mpls(tb, &key->mpls, &mask->mpls);
  795. if (ret)
  796. return ret;
  797. } else if (key->basic.n_proto == htons(ETH_P_ARP) ||
  798. key->basic.n_proto == htons(ETH_P_RARP)) {
  799. fl_set_key_val(tb, &key->arp.sip, TCA_FLOWER_KEY_ARP_SIP,
  800. &mask->arp.sip, TCA_FLOWER_KEY_ARP_SIP_MASK,
  801. sizeof(key->arp.sip));
  802. fl_set_key_val(tb, &key->arp.tip, TCA_FLOWER_KEY_ARP_TIP,
  803. &mask->arp.tip, TCA_FLOWER_KEY_ARP_TIP_MASK,
  804. sizeof(key->arp.tip));
  805. fl_set_key_val(tb, &key->arp.op, TCA_FLOWER_KEY_ARP_OP,
  806. &mask->arp.op, TCA_FLOWER_KEY_ARP_OP_MASK,
  807. sizeof(key->arp.op));
  808. fl_set_key_val(tb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
  809. mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
  810. sizeof(key->arp.sha));
  811. fl_set_key_val(tb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
  812. mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
  813. sizeof(key->arp.tha));
  814. }
  815. if (tb[TCA_FLOWER_KEY_ENC_IPV4_SRC] ||
  816. tb[TCA_FLOWER_KEY_ENC_IPV4_DST]) {
  817. key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  818. mask->enc_control.addr_type = ~0;
  819. fl_set_key_val(tb, &key->enc_ipv4.src,
  820. TCA_FLOWER_KEY_ENC_IPV4_SRC,
  821. &mask->enc_ipv4.src,
  822. TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
  823. sizeof(key->enc_ipv4.src));
  824. fl_set_key_val(tb, &key->enc_ipv4.dst,
  825. TCA_FLOWER_KEY_ENC_IPV4_DST,
  826. &mask->enc_ipv4.dst,
  827. TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
  828. sizeof(key->enc_ipv4.dst));
  829. }
  830. if (tb[TCA_FLOWER_KEY_ENC_IPV6_SRC] ||
  831. tb[TCA_FLOWER_KEY_ENC_IPV6_DST]) {
  832. key->enc_control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  833. mask->enc_control.addr_type = ~0;
  834. fl_set_key_val(tb, &key->enc_ipv6.src,
  835. TCA_FLOWER_KEY_ENC_IPV6_SRC,
  836. &mask->enc_ipv6.src,
  837. TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
  838. sizeof(key->enc_ipv6.src));
  839. fl_set_key_val(tb, &key->enc_ipv6.dst,
  840. TCA_FLOWER_KEY_ENC_IPV6_DST,
  841. &mask->enc_ipv6.dst,
  842. TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
  843. sizeof(key->enc_ipv6.dst));
  844. }
  845. fl_set_key_val(tb, &key->enc_key_id.keyid, TCA_FLOWER_KEY_ENC_KEY_ID,
  846. &mask->enc_key_id.keyid, TCA_FLOWER_UNSPEC,
  847. sizeof(key->enc_key_id.keyid));
  848. fl_set_key_val(tb, &key->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
  849. &mask->enc_tp.src, TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
  850. sizeof(key->enc_tp.src));
  851. fl_set_key_val(tb, &key->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
  852. &mask->enc_tp.dst, TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
  853. sizeof(key->enc_tp.dst));
  854. fl_set_key_ip(tb, true, &key->enc_ip, &mask->enc_ip);
  855. if (tb[TCA_FLOWER_KEY_ENC_OPTS]) {
  856. ret = fl_set_enc_opt(tb, key, mask, extack);
  857. if (ret)
  858. return ret;
  859. }
  860. if (tb[TCA_FLOWER_KEY_FLAGS])
  861. ret = fl_set_key_flags(tb, &key->control.flags, &mask->control.flags);
  862. return ret;
  863. }
  864. static void fl_mask_copy(struct fl_flow_mask *dst,
  865. struct fl_flow_mask *src)
  866. {
  867. const void *psrc = fl_key_get_start(&src->key, src);
  868. void *pdst = fl_key_get_start(&dst->key, src);
  869. memcpy(pdst, psrc, fl_mask_range(src));
  870. dst->range = src->range;
  871. }
  872. static const struct rhashtable_params fl_ht_params = {
  873. .key_offset = offsetof(struct cls_fl_filter, mkey), /* base offset */
  874. .head_offset = offsetof(struct cls_fl_filter, ht_node),
  875. .automatic_shrinking = true,
  876. };
  877. static int fl_init_mask_hashtable(struct fl_flow_mask *mask)
  878. {
  879. mask->filter_ht_params = fl_ht_params;
  880. mask->filter_ht_params.key_len = fl_mask_range(mask);
  881. mask->filter_ht_params.key_offset += mask->range.start;
  882. return rhashtable_init(&mask->ht, &mask->filter_ht_params);
  883. }
  884. #define FL_KEY_MEMBER_OFFSET(member) offsetof(struct fl_flow_key, member)
  885. #define FL_KEY_MEMBER_SIZE(member) (sizeof(((struct fl_flow_key *) 0)->member))
  886. #define FL_KEY_IS_MASKED(mask, member) \
  887. memchr_inv(((char *)mask) + FL_KEY_MEMBER_OFFSET(member), \
  888. 0, FL_KEY_MEMBER_SIZE(member)) \
  889. #define FL_KEY_SET(keys, cnt, id, member) \
  890. do { \
  891. keys[cnt].key_id = id; \
  892. keys[cnt].offset = FL_KEY_MEMBER_OFFSET(member); \
  893. cnt++; \
  894. } while(0);
  895. #define FL_KEY_SET_IF_MASKED(mask, keys, cnt, id, member) \
  896. do { \
  897. if (FL_KEY_IS_MASKED(mask, member)) \
  898. FL_KEY_SET(keys, cnt, id, member); \
  899. } while(0);
  900. static void fl_init_dissector(struct flow_dissector *dissector,
  901. struct fl_flow_key *mask)
  902. {
  903. struct flow_dissector_key keys[FLOW_DISSECTOR_KEY_MAX];
  904. size_t cnt = 0;
  905. FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_CONTROL, control);
  906. FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_BASIC, basic);
  907. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  908. FLOW_DISSECTOR_KEY_ETH_ADDRS, eth);
  909. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  910. FLOW_DISSECTOR_KEY_IPV4_ADDRS, ipv4);
  911. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  912. FLOW_DISSECTOR_KEY_IPV6_ADDRS, ipv6);
  913. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  914. FLOW_DISSECTOR_KEY_PORTS, tp);
  915. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  916. FLOW_DISSECTOR_KEY_IP, ip);
  917. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  918. FLOW_DISSECTOR_KEY_TCP, tcp);
  919. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  920. FLOW_DISSECTOR_KEY_ICMP, icmp);
  921. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  922. FLOW_DISSECTOR_KEY_ARP, arp);
  923. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  924. FLOW_DISSECTOR_KEY_MPLS, mpls);
  925. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  926. FLOW_DISSECTOR_KEY_VLAN, vlan);
  927. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  928. FLOW_DISSECTOR_KEY_CVLAN, cvlan);
  929. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  930. FLOW_DISSECTOR_KEY_ENC_KEYID, enc_key_id);
  931. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  932. FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS, enc_ipv4);
  933. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  934. FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS, enc_ipv6);
  935. if (FL_KEY_IS_MASKED(mask, enc_ipv4) ||
  936. FL_KEY_IS_MASKED(mask, enc_ipv6))
  937. FL_KEY_SET(keys, cnt, FLOW_DISSECTOR_KEY_ENC_CONTROL,
  938. enc_control);
  939. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  940. FLOW_DISSECTOR_KEY_ENC_PORTS, enc_tp);
  941. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  942. FLOW_DISSECTOR_KEY_ENC_IP, enc_ip);
  943. FL_KEY_SET_IF_MASKED(mask, keys, cnt,
  944. FLOW_DISSECTOR_KEY_ENC_OPTS, enc_opts);
  945. skb_flow_dissector_init(dissector, keys, cnt);
  946. }
  947. static struct fl_flow_mask *fl_create_new_mask(struct cls_fl_head *head,
  948. struct fl_flow_mask *mask)
  949. {
  950. struct fl_flow_mask *newmask;
  951. int err;
  952. newmask = kzalloc(sizeof(*newmask), GFP_KERNEL);
  953. if (!newmask)
  954. return ERR_PTR(-ENOMEM);
  955. fl_mask_copy(newmask, mask);
  956. err = fl_init_mask_hashtable(newmask);
  957. if (err)
  958. goto errout_free;
  959. fl_init_dissector(&newmask->dissector, &newmask->key);
  960. INIT_LIST_HEAD_RCU(&newmask->filters);
  961. err = rhashtable_insert_fast(&head->ht, &newmask->ht_node,
  962. mask_ht_params);
  963. if (err)
  964. goto errout_destroy;
  965. list_add_tail_rcu(&newmask->list, &head->masks);
  966. return newmask;
  967. errout_destroy:
  968. rhashtable_destroy(&newmask->ht);
  969. errout_free:
  970. kfree(newmask);
  971. return ERR_PTR(err);
  972. }
  973. static int fl_check_assign_mask(struct cls_fl_head *head,
  974. struct cls_fl_filter *fnew,
  975. struct cls_fl_filter *fold,
  976. struct fl_flow_mask *mask)
  977. {
  978. struct fl_flow_mask *newmask;
  979. fnew->mask = rhashtable_lookup_fast(&head->ht, mask, mask_ht_params);
  980. if (!fnew->mask) {
  981. if (fold)
  982. return -EINVAL;
  983. newmask = fl_create_new_mask(head, mask);
  984. if (IS_ERR(newmask))
  985. return PTR_ERR(newmask);
  986. fnew->mask = newmask;
  987. } else if (fold && fold->mask != fnew->mask) {
  988. return -EINVAL;
  989. }
  990. return 0;
  991. }
  992. static int fl_set_parms(struct net *net, struct tcf_proto *tp,
  993. struct cls_fl_filter *f, struct fl_flow_mask *mask,
  994. unsigned long base, struct nlattr **tb,
  995. struct nlattr *est, bool ovr,
  996. struct fl_flow_tmplt *tmplt,
  997. struct netlink_ext_ack *extack)
  998. {
  999. int err;
  1000. err = tcf_exts_validate(net, tp, tb, est, &f->exts, ovr, extack);
  1001. if (err < 0)
  1002. return err;
  1003. if (tb[TCA_FLOWER_CLASSID]) {
  1004. f->res.classid = nla_get_u32(tb[TCA_FLOWER_CLASSID]);
  1005. tcf_bind_filter(tp, &f->res, base);
  1006. }
  1007. err = fl_set_key(net, tb, &f->key, &mask->key, extack);
  1008. if (err)
  1009. return err;
  1010. fl_mask_update_range(mask);
  1011. fl_set_masked_key(&f->mkey, &f->key, mask);
  1012. if (!fl_mask_fits_tmplt(tmplt, mask)) {
  1013. NL_SET_ERR_MSG_MOD(extack, "Mask does not fit the template");
  1014. return -EINVAL;
  1015. }
  1016. return 0;
  1017. }
  1018. static int fl_change(struct net *net, struct sk_buff *in_skb,
  1019. struct tcf_proto *tp, unsigned long base,
  1020. u32 handle, struct nlattr **tca,
  1021. void **arg, bool ovr, struct netlink_ext_ack *extack)
  1022. {
  1023. struct cls_fl_head *head = rtnl_dereference(tp->root);
  1024. struct cls_fl_filter *fold = *arg;
  1025. struct cls_fl_filter *fnew;
  1026. struct fl_flow_mask *mask;
  1027. struct nlattr **tb;
  1028. int err;
  1029. if (!tca[TCA_OPTIONS])
  1030. return -EINVAL;
  1031. mask = kzalloc(sizeof(struct fl_flow_mask), GFP_KERNEL);
  1032. if (!mask)
  1033. return -ENOBUFS;
  1034. tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
  1035. if (!tb) {
  1036. err = -ENOBUFS;
  1037. goto errout_mask_alloc;
  1038. }
  1039. err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
  1040. fl_policy, NULL);
  1041. if (err < 0)
  1042. goto errout_tb;
  1043. if (fold && handle && fold->handle != handle) {
  1044. err = -EINVAL;
  1045. goto errout_tb;
  1046. }
  1047. fnew = kzalloc(sizeof(*fnew), GFP_KERNEL);
  1048. if (!fnew) {
  1049. err = -ENOBUFS;
  1050. goto errout_tb;
  1051. }
  1052. err = tcf_exts_init(&fnew->exts, TCA_FLOWER_ACT, 0);
  1053. if (err < 0)
  1054. goto errout;
  1055. if (tb[TCA_FLOWER_FLAGS]) {
  1056. fnew->flags = nla_get_u32(tb[TCA_FLOWER_FLAGS]);
  1057. if (!tc_flags_valid(fnew->flags)) {
  1058. err = -EINVAL;
  1059. goto errout;
  1060. }
  1061. }
  1062. err = fl_set_parms(net, tp, fnew, mask, base, tb, tca[TCA_RATE], ovr,
  1063. tp->chain->tmplt_priv, extack);
  1064. if (err)
  1065. goto errout;
  1066. err = fl_check_assign_mask(head, fnew, fold, mask);
  1067. if (err)
  1068. goto errout;
  1069. if (!handle) {
  1070. handle = 1;
  1071. err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
  1072. INT_MAX, GFP_KERNEL);
  1073. } else if (!fold) {
  1074. /* user specifies a handle and it doesn't exist */
  1075. err = idr_alloc_u32(&head->handle_idr, fnew, &handle,
  1076. handle, GFP_KERNEL);
  1077. }
  1078. if (err)
  1079. goto errout_mask;
  1080. fnew->handle = handle;
  1081. if (!tc_skip_sw(fnew->flags)) {
  1082. if (!fold && fl_lookup(fnew->mask, &fnew->mkey)) {
  1083. err = -EEXIST;
  1084. goto errout_idr;
  1085. }
  1086. err = rhashtable_insert_fast(&fnew->mask->ht, &fnew->ht_node,
  1087. fnew->mask->filter_ht_params);
  1088. if (err)
  1089. goto errout_idr;
  1090. }
  1091. if (!tc_skip_hw(fnew->flags)) {
  1092. err = fl_hw_replace_filter(tp, fnew, extack);
  1093. if (err)
  1094. goto errout_mask;
  1095. }
  1096. if (!tc_in_hw(fnew->flags))
  1097. fnew->flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  1098. if (fold) {
  1099. if (!tc_skip_sw(fold->flags))
  1100. rhashtable_remove_fast(&fold->mask->ht,
  1101. &fold->ht_node,
  1102. fold->mask->filter_ht_params);
  1103. if (!tc_skip_hw(fold->flags))
  1104. fl_hw_destroy_filter(tp, fold, NULL);
  1105. }
  1106. *arg = fnew;
  1107. if (fold) {
  1108. idr_replace(&head->handle_idr, fnew, fnew->handle);
  1109. list_replace_rcu(&fold->list, &fnew->list);
  1110. tcf_unbind_filter(tp, &fold->res);
  1111. tcf_exts_get_net(&fold->exts);
  1112. tcf_queue_work(&fold->rwork, fl_destroy_filter_work);
  1113. } else {
  1114. list_add_tail_rcu(&fnew->list, &fnew->mask->filters);
  1115. }
  1116. kfree(tb);
  1117. kfree(mask);
  1118. return 0;
  1119. errout_idr:
  1120. if (!fold)
  1121. idr_remove(&head->handle_idr, fnew->handle);
  1122. errout_mask:
  1123. fl_mask_put(head, fnew->mask, false);
  1124. errout:
  1125. tcf_exts_destroy(&fnew->exts);
  1126. kfree(fnew);
  1127. errout_tb:
  1128. kfree(tb);
  1129. errout_mask_alloc:
  1130. kfree(mask);
  1131. return err;
  1132. }
  1133. static int fl_delete(struct tcf_proto *tp, void *arg, bool *last,
  1134. struct netlink_ext_ack *extack)
  1135. {
  1136. struct cls_fl_head *head = rtnl_dereference(tp->root);
  1137. struct cls_fl_filter *f = arg;
  1138. if (!tc_skip_sw(f->flags))
  1139. rhashtable_remove_fast(&f->mask->ht, &f->ht_node,
  1140. f->mask->filter_ht_params);
  1141. __fl_delete(tp, f, extack);
  1142. *last = list_empty(&head->masks);
  1143. return 0;
  1144. }
  1145. static void fl_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  1146. {
  1147. struct cls_fl_head *head = rtnl_dereference(tp->root);
  1148. struct cls_fl_filter *f;
  1149. arg->count = arg->skip;
  1150. while ((f = idr_get_next_ul(&head->handle_idr,
  1151. &arg->cookie)) != NULL) {
  1152. if (arg->fn(tp, f, arg) < 0) {
  1153. arg->stop = 1;
  1154. break;
  1155. }
  1156. arg->cookie = f->handle + 1;
  1157. arg->count++;
  1158. }
  1159. }
  1160. static int fl_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
  1161. void *cb_priv, struct netlink_ext_ack *extack)
  1162. {
  1163. struct cls_fl_head *head = rtnl_dereference(tp->root);
  1164. struct tc_cls_flower_offload cls_flower = {};
  1165. struct tcf_block *block = tp->chain->block;
  1166. struct fl_flow_mask *mask;
  1167. struct cls_fl_filter *f;
  1168. int err;
  1169. list_for_each_entry(mask, &head->masks, list) {
  1170. list_for_each_entry(f, &mask->filters, list) {
  1171. if (tc_skip_hw(f->flags))
  1172. continue;
  1173. tc_cls_common_offload_init(&cls_flower.common, tp,
  1174. f->flags, extack);
  1175. cls_flower.command = add ?
  1176. TC_CLSFLOWER_REPLACE : TC_CLSFLOWER_DESTROY;
  1177. cls_flower.cookie = (unsigned long)f;
  1178. cls_flower.dissector = &mask->dissector;
  1179. cls_flower.mask = &mask->key;
  1180. cls_flower.key = &f->mkey;
  1181. cls_flower.exts = &f->exts;
  1182. cls_flower.classid = f->res.classid;
  1183. err = cb(TC_SETUP_CLSFLOWER, &cls_flower, cb_priv);
  1184. if (err) {
  1185. if (add && tc_skip_sw(f->flags))
  1186. return err;
  1187. continue;
  1188. }
  1189. tc_cls_offload_cnt_update(block, &f->in_hw_count,
  1190. &f->flags, add);
  1191. }
  1192. }
  1193. return 0;
  1194. }
  1195. static void fl_hw_create_tmplt(struct tcf_chain *chain,
  1196. struct fl_flow_tmplt *tmplt)
  1197. {
  1198. struct tc_cls_flower_offload cls_flower = {};
  1199. struct tcf_block *block = chain->block;
  1200. struct tcf_exts dummy_exts = { 0, };
  1201. cls_flower.common.chain_index = chain->index;
  1202. cls_flower.command = TC_CLSFLOWER_TMPLT_CREATE;
  1203. cls_flower.cookie = (unsigned long) tmplt;
  1204. cls_flower.dissector = &tmplt->dissector;
  1205. cls_flower.mask = &tmplt->mask;
  1206. cls_flower.key = &tmplt->dummy_key;
  1207. cls_flower.exts = &dummy_exts;
  1208. /* We don't care if driver (any of them) fails to handle this
  1209. * call. It serves just as a hint for it.
  1210. */
  1211. tc_setup_cb_call(block, NULL, TC_SETUP_CLSFLOWER,
  1212. &cls_flower, false);
  1213. }
  1214. static void fl_hw_destroy_tmplt(struct tcf_chain *chain,
  1215. struct fl_flow_tmplt *tmplt)
  1216. {
  1217. struct tc_cls_flower_offload cls_flower = {};
  1218. struct tcf_block *block = chain->block;
  1219. cls_flower.common.chain_index = chain->index;
  1220. cls_flower.command = TC_CLSFLOWER_TMPLT_DESTROY;
  1221. cls_flower.cookie = (unsigned long) tmplt;
  1222. tc_setup_cb_call(block, NULL, TC_SETUP_CLSFLOWER,
  1223. &cls_flower, false);
  1224. }
  1225. static void *fl_tmplt_create(struct net *net, struct tcf_chain *chain,
  1226. struct nlattr **tca,
  1227. struct netlink_ext_ack *extack)
  1228. {
  1229. struct fl_flow_tmplt *tmplt;
  1230. struct nlattr **tb;
  1231. int err;
  1232. if (!tca[TCA_OPTIONS])
  1233. return ERR_PTR(-EINVAL);
  1234. tb = kcalloc(TCA_FLOWER_MAX + 1, sizeof(struct nlattr *), GFP_KERNEL);
  1235. if (!tb)
  1236. return ERR_PTR(-ENOBUFS);
  1237. err = nla_parse_nested(tb, TCA_FLOWER_MAX, tca[TCA_OPTIONS],
  1238. fl_policy, NULL);
  1239. if (err)
  1240. goto errout_tb;
  1241. tmplt = kzalloc(sizeof(*tmplt), GFP_KERNEL);
  1242. if (!tmplt) {
  1243. err = -ENOMEM;
  1244. goto errout_tb;
  1245. }
  1246. tmplt->chain = chain;
  1247. err = fl_set_key(net, tb, &tmplt->dummy_key, &tmplt->mask, extack);
  1248. if (err)
  1249. goto errout_tmplt;
  1250. kfree(tb);
  1251. fl_init_dissector(&tmplt->dissector, &tmplt->mask);
  1252. fl_hw_create_tmplt(chain, tmplt);
  1253. return tmplt;
  1254. errout_tmplt:
  1255. kfree(tmplt);
  1256. errout_tb:
  1257. kfree(tb);
  1258. return ERR_PTR(err);
  1259. }
  1260. static void fl_tmplt_destroy(void *tmplt_priv)
  1261. {
  1262. struct fl_flow_tmplt *tmplt = tmplt_priv;
  1263. fl_hw_destroy_tmplt(tmplt->chain, tmplt);
  1264. kfree(tmplt);
  1265. }
  1266. static int fl_dump_key_val(struct sk_buff *skb,
  1267. void *val, int val_type,
  1268. void *mask, int mask_type, int len)
  1269. {
  1270. int err;
  1271. if (!memchr_inv(mask, 0, len))
  1272. return 0;
  1273. err = nla_put(skb, val_type, len, val);
  1274. if (err)
  1275. return err;
  1276. if (mask_type != TCA_FLOWER_UNSPEC) {
  1277. err = nla_put(skb, mask_type, len, mask);
  1278. if (err)
  1279. return err;
  1280. }
  1281. return 0;
  1282. }
  1283. static int fl_dump_key_mpls(struct sk_buff *skb,
  1284. struct flow_dissector_key_mpls *mpls_key,
  1285. struct flow_dissector_key_mpls *mpls_mask)
  1286. {
  1287. int err;
  1288. if (!memchr_inv(mpls_mask, 0, sizeof(*mpls_mask)))
  1289. return 0;
  1290. if (mpls_mask->mpls_ttl) {
  1291. err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TTL,
  1292. mpls_key->mpls_ttl);
  1293. if (err)
  1294. return err;
  1295. }
  1296. if (mpls_mask->mpls_tc) {
  1297. err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_TC,
  1298. mpls_key->mpls_tc);
  1299. if (err)
  1300. return err;
  1301. }
  1302. if (mpls_mask->mpls_label) {
  1303. err = nla_put_u32(skb, TCA_FLOWER_KEY_MPLS_LABEL,
  1304. mpls_key->mpls_label);
  1305. if (err)
  1306. return err;
  1307. }
  1308. if (mpls_mask->mpls_bos) {
  1309. err = nla_put_u8(skb, TCA_FLOWER_KEY_MPLS_BOS,
  1310. mpls_key->mpls_bos);
  1311. if (err)
  1312. return err;
  1313. }
  1314. return 0;
  1315. }
  1316. static int fl_dump_key_ip(struct sk_buff *skb, bool encap,
  1317. struct flow_dissector_key_ip *key,
  1318. struct flow_dissector_key_ip *mask)
  1319. {
  1320. int tos_key = encap ? TCA_FLOWER_KEY_ENC_IP_TOS : TCA_FLOWER_KEY_IP_TOS;
  1321. int ttl_key = encap ? TCA_FLOWER_KEY_ENC_IP_TTL : TCA_FLOWER_KEY_IP_TTL;
  1322. int tos_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TOS_MASK : TCA_FLOWER_KEY_IP_TOS_MASK;
  1323. int ttl_mask = encap ? TCA_FLOWER_KEY_ENC_IP_TTL_MASK : TCA_FLOWER_KEY_IP_TTL_MASK;
  1324. if (fl_dump_key_val(skb, &key->tos, tos_key, &mask->tos, tos_mask, sizeof(key->tos)) ||
  1325. fl_dump_key_val(skb, &key->ttl, ttl_key, &mask->ttl, ttl_mask, sizeof(key->ttl)))
  1326. return -1;
  1327. return 0;
  1328. }
  1329. static int fl_dump_key_vlan(struct sk_buff *skb,
  1330. int vlan_id_key, int vlan_prio_key,
  1331. struct flow_dissector_key_vlan *vlan_key,
  1332. struct flow_dissector_key_vlan *vlan_mask)
  1333. {
  1334. int err;
  1335. if (!memchr_inv(vlan_mask, 0, sizeof(*vlan_mask)))
  1336. return 0;
  1337. if (vlan_mask->vlan_id) {
  1338. err = nla_put_u16(skb, vlan_id_key,
  1339. vlan_key->vlan_id);
  1340. if (err)
  1341. return err;
  1342. }
  1343. if (vlan_mask->vlan_priority) {
  1344. err = nla_put_u8(skb, vlan_prio_key,
  1345. vlan_key->vlan_priority);
  1346. if (err)
  1347. return err;
  1348. }
  1349. return 0;
  1350. }
  1351. static void fl_get_key_flag(u32 dissector_key, u32 dissector_mask,
  1352. u32 *flower_key, u32 *flower_mask,
  1353. u32 flower_flag_bit, u32 dissector_flag_bit)
  1354. {
  1355. if (dissector_mask & dissector_flag_bit) {
  1356. *flower_mask |= flower_flag_bit;
  1357. if (dissector_key & dissector_flag_bit)
  1358. *flower_key |= flower_flag_bit;
  1359. }
  1360. }
  1361. static int fl_dump_key_flags(struct sk_buff *skb, u32 flags_key, u32 flags_mask)
  1362. {
  1363. u32 key, mask;
  1364. __be32 _key, _mask;
  1365. int err;
  1366. if (!memchr_inv(&flags_mask, 0, sizeof(flags_mask)))
  1367. return 0;
  1368. key = 0;
  1369. mask = 0;
  1370. fl_get_key_flag(flags_key, flags_mask, &key, &mask,
  1371. TCA_FLOWER_KEY_FLAGS_IS_FRAGMENT, FLOW_DIS_IS_FRAGMENT);
  1372. fl_get_key_flag(flags_key, flags_mask, &key, &mask,
  1373. TCA_FLOWER_KEY_FLAGS_FRAG_IS_FIRST,
  1374. FLOW_DIS_FIRST_FRAG);
  1375. _key = cpu_to_be32(key);
  1376. _mask = cpu_to_be32(mask);
  1377. err = nla_put(skb, TCA_FLOWER_KEY_FLAGS, 4, &_key);
  1378. if (err)
  1379. return err;
  1380. return nla_put(skb, TCA_FLOWER_KEY_FLAGS_MASK, 4, &_mask);
  1381. }
  1382. static int fl_dump_key_geneve_opt(struct sk_buff *skb,
  1383. struct flow_dissector_key_enc_opts *enc_opts)
  1384. {
  1385. struct geneve_opt *opt;
  1386. struct nlattr *nest;
  1387. int opt_off = 0;
  1388. nest = nla_nest_start(skb, TCA_FLOWER_KEY_ENC_OPTS_GENEVE);
  1389. if (!nest)
  1390. goto nla_put_failure;
  1391. while (enc_opts->len > opt_off) {
  1392. opt = (struct geneve_opt *)&enc_opts->data[opt_off];
  1393. if (nla_put_be16(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_CLASS,
  1394. opt->opt_class))
  1395. goto nla_put_failure;
  1396. if (nla_put_u8(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_TYPE,
  1397. opt->type))
  1398. goto nla_put_failure;
  1399. if (nla_put(skb, TCA_FLOWER_KEY_ENC_OPT_GENEVE_DATA,
  1400. opt->length * 4, opt->opt_data))
  1401. goto nla_put_failure;
  1402. opt_off += sizeof(struct geneve_opt) + opt->length * 4;
  1403. }
  1404. nla_nest_end(skb, nest);
  1405. return 0;
  1406. nla_put_failure:
  1407. nla_nest_cancel(skb, nest);
  1408. return -EMSGSIZE;
  1409. }
  1410. static int fl_dump_key_options(struct sk_buff *skb, int enc_opt_type,
  1411. struct flow_dissector_key_enc_opts *enc_opts)
  1412. {
  1413. struct nlattr *nest;
  1414. int err;
  1415. if (!enc_opts->len)
  1416. return 0;
  1417. nest = nla_nest_start(skb, enc_opt_type);
  1418. if (!nest)
  1419. goto nla_put_failure;
  1420. switch (enc_opts->dst_opt_type) {
  1421. case TUNNEL_GENEVE_OPT:
  1422. err = fl_dump_key_geneve_opt(skb, enc_opts);
  1423. if (err)
  1424. goto nla_put_failure;
  1425. break;
  1426. default:
  1427. goto nla_put_failure;
  1428. }
  1429. nla_nest_end(skb, nest);
  1430. return 0;
  1431. nla_put_failure:
  1432. nla_nest_cancel(skb, nest);
  1433. return -EMSGSIZE;
  1434. }
  1435. static int fl_dump_key_enc_opt(struct sk_buff *skb,
  1436. struct flow_dissector_key_enc_opts *key_opts,
  1437. struct flow_dissector_key_enc_opts *msk_opts)
  1438. {
  1439. int err;
  1440. err = fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS, key_opts);
  1441. if (err)
  1442. return err;
  1443. return fl_dump_key_options(skb, TCA_FLOWER_KEY_ENC_OPTS_MASK, msk_opts);
  1444. }
  1445. static int fl_dump_key(struct sk_buff *skb, struct net *net,
  1446. struct fl_flow_key *key, struct fl_flow_key *mask)
  1447. {
  1448. if (mask->indev_ifindex) {
  1449. struct net_device *dev;
  1450. dev = __dev_get_by_index(net, key->indev_ifindex);
  1451. if (dev && nla_put_string(skb, TCA_FLOWER_INDEV, dev->name))
  1452. goto nla_put_failure;
  1453. }
  1454. if (fl_dump_key_val(skb, key->eth.dst, TCA_FLOWER_KEY_ETH_DST,
  1455. mask->eth.dst, TCA_FLOWER_KEY_ETH_DST_MASK,
  1456. sizeof(key->eth.dst)) ||
  1457. fl_dump_key_val(skb, key->eth.src, TCA_FLOWER_KEY_ETH_SRC,
  1458. mask->eth.src, TCA_FLOWER_KEY_ETH_SRC_MASK,
  1459. sizeof(key->eth.src)) ||
  1460. fl_dump_key_val(skb, &key->basic.n_proto, TCA_FLOWER_KEY_ETH_TYPE,
  1461. &mask->basic.n_proto, TCA_FLOWER_UNSPEC,
  1462. sizeof(key->basic.n_proto)))
  1463. goto nla_put_failure;
  1464. if (fl_dump_key_mpls(skb, &key->mpls, &mask->mpls))
  1465. goto nla_put_failure;
  1466. if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_VLAN_ID,
  1467. TCA_FLOWER_KEY_VLAN_PRIO, &key->vlan, &mask->vlan))
  1468. goto nla_put_failure;
  1469. if (fl_dump_key_vlan(skb, TCA_FLOWER_KEY_CVLAN_ID,
  1470. TCA_FLOWER_KEY_CVLAN_PRIO,
  1471. &key->cvlan, &mask->cvlan) ||
  1472. (mask->cvlan.vlan_tpid &&
  1473. nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
  1474. key->cvlan.vlan_tpid)))
  1475. goto nla_put_failure;
  1476. if (mask->basic.n_proto) {
  1477. if (mask->cvlan.vlan_tpid) {
  1478. if (nla_put_be16(skb, TCA_FLOWER_KEY_CVLAN_ETH_TYPE,
  1479. key->basic.n_proto))
  1480. goto nla_put_failure;
  1481. } else if (mask->vlan.vlan_tpid) {
  1482. if (nla_put_be16(skb, TCA_FLOWER_KEY_VLAN_ETH_TYPE,
  1483. key->basic.n_proto))
  1484. goto nla_put_failure;
  1485. }
  1486. }
  1487. if ((key->basic.n_proto == htons(ETH_P_IP) ||
  1488. key->basic.n_proto == htons(ETH_P_IPV6)) &&
  1489. (fl_dump_key_val(skb, &key->basic.ip_proto, TCA_FLOWER_KEY_IP_PROTO,
  1490. &mask->basic.ip_proto, TCA_FLOWER_UNSPEC,
  1491. sizeof(key->basic.ip_proto)) ||
  1492. fl_dump_key_ip(skb, false, &key->ip, &mask->ip)))
  1493. goto nla_put_failure;
  1494. if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
  1495. (fl_dump_key_val(skb, &key->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC,
  1496. &mask->ipv4.src, TCA_FLOWER_KEY_IPV4_SRC_MASK,
  1497. sizeof(key->ipv4.src)) ||
  1498. fl_dump_key_val(skb, &key->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST,
  1499. &mask->ipv4.dst, TCA_FLOWER_KEY_IPV4_DST_MASK,
  1500. sizeof(key->ipv4.dst))))
  1501. goto nla_put_failure;
  1502. else if (key->control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
  1503. (fl_dump_key_val(skb, &key->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC,
  1504. &mask->ipv6.src, TCA_FLOWER_KEY_IPV6_SRC_MASK,
  1505. sizeof(key->ipv6.src)) ||
  1506. fl_dump_key_val(skb, &key->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST,
  1507. &mask->ipv6.dst, TCA_FLOWER_KEY_IPV6_DST_MASK,
  1508. sizeof(key->ipv6.dst))))
  1509. goto nla_put_failure;
  1510. if (key->basic.ip_proto == IPPROTO_TCP &&
  1511. (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_TCP_SRC,
  1512. &mask->tp.src, TCA_FLOWER_KEY_TCP_SRC_MASK,
  1513. sizeof(key->tp.src)) ||
  1514. fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_TCP_DST,
  1515. &mask->tp.dst, TCA_FLOWER_KEY_TCP_DST_MASK,
  1516. sizeof(key->tp.dst)) ||
  1517. fl_dump_key_val(skb, &key->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS,
  1518. &mask->tcp.flags, TCA_FLOWER_KEY_TCP_FLAGS_MASK,
  1519. sizeof(key->tcp.flags))))
  1520. goto nla_put_failure;
  1521. else if (key->basic.ip_proto == IPPROTO_UDP &&
  1522. (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_UDP_SRC,
  1523. &mask->tp.src, TCA_FLOWER_KEY_UDP_SRC_MASK,
  1524. sizeof(key->tp.src)) ||
  1525. fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_UDP_DST,
  1526. &mask->tp.dst, TCA_FLOWER_KEY_UDP_DST_MASK,
  1527. sizeof(key->tp.dst))))
  1528. goto nla_put_failure;
  1529. else if (key->basic.ip_proto == IPPROTO_SCTP &&
  1530. (fl_dump_key_val(skb, &key->tp.src, TCA_FLOWER_KEY_SCTP_SRC,
  1531. &mask->tp.src, TCA_FLOWER_KEY_SCTP_SRC_MASK,
  1532. sizeof(key->tp.src)) ||
  1533. fl_dump_key_val(skb, &key->tp.dst, TCA_FLOWER_KEY_SCTP_DST,
  1534. &mask->tp.dst, TCA_FLOWER_KEY_SCTP_DST_MASK,
  1535. sizeof(key->tp.dst))))
  1536. goto nla_put_failure;
  1537. else if (key->basic.n_proto == htons(ETH_P_IP) &&
  1538. key->basic.ip_proto == IPPROTO_ICMP &&
  1539. (fl_dump_key_val(skb, &key->icmp.type,
  1540. TCA_FLOWER_KEY_ICMPV4_TYPE, &mask->icmp.type,
  1541. TCA_FLOWER_KEY_ICMPV4_TYPE_MASK,
  1542. sizeof(key->icmp.type)) ||
  1543. fl_dump_key_val(skb, &key->icmp.code,
  1544. TCA_FLOWER_KEY_ICMPV4_CODE, &mask->icmp.code,
  1545. TCA_FLOWER_KEY_ICMPV4_CODE_MASK,
  1546. sizeof(key->icmp.code))))
  1547. goto nla_put_failure;
  1548. else if (key->basic.n_proto == htons(ETH_P_IPV6) &&
  1549. key->basic.ip_proto == IPPROTO_ICMPV6 &&
  1550. (fl_dump_key_val(skb, &key->icmp.type,
  1551. TCA_FLOWER_KEY_ICMPV6_TYPE, &mask->icmp.type,
  1552. TCA_FLOWER_KEY_ICMPV6_TYPE_MASK,
  1553. sizeof(key->icmp.type)) ||
  1554. fl_dump_key_val(skb, &key->icmp.code,
  1555. TCA_FLOWER_KEY_ICMPV6_CODE, &mask->icmp.code,
  1556. TCA_FLOWER_KEY_ICMPV6_CODE_MASK,
  1557. sizeof(key->icmp.code))))
  1558. goto nla_put_failure;
  1559. else if ((key->basic.n_proto == htons(ETH_P_ARP) ||
  1560. key->basic.n_proto == htons(ETH_P_RARP)) &&
  1561. (fl_dump_key_val(skb, &key->arp.sip,
  1562. TCA_FLOWER_KEY_ARP_SIP, &mask->arp.sip,
  1563. TCA_FLOWER_KEY_ARP_SIP_MASK,
  1564. sizeof(key->arp.sip)) ||
  1565. fl_dump_key_val(skb, &key->arp.tip,
  1566. TCA_FLOWER_KEY_ARP_TIP, &mask->arp.tip,
  1567. TCA_FLOWER_KEY_ARP_TIP_MASK,
  1568. sizeof(key->arp.tip)) ||
  1569. fl_dump_key_val(skb, &key->arp.op,
  1570. TCA_FLOWER_KEY_ARP_OP, &mask->arp.op,
  1571. TCA_FLOWER_KEY_ARP_OP_MASK,
  1572. sizeof(key->arp.op)) ||
  1573. fl_dump_key_val(skb, key->arp.sha, TCA_FLOWER_KEY_ARP_SHA,
  1574. mask->arp.sha, TCA_FLOWER_KEY_ARP_SHA_MASK,
  1575. sizeof(key->arp.sha)) ||
  1576. fl_dump_key_val(skb, key->arp.tha, TCA_FLOWER_KEY_ARP_THA,
  1577. mask->arp.tha, TCA_FLOWER_KEY_ARP_THA_MASK,
  1578. sizeof(key->arp.tha))))
  1579. goto nla_put_failure;
  1580. if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS &&
  1581. (fl_dump_key_val(skb, &key->enc_ipv4.src,
  1582. TCA_FLOWER_KEY_ENC_IPV4_SRC, &mask->enc_ipv4.src,
  1583. TCA_FLOWER_KEY_ENC_IPV4_SRC_MASK,
  1584. sizeof(key->enc_ipv4.src)) ||
  1585. fl_dump_key_val(skb, &key->enc_ipv4.dst,
  1586. TCA_FLOWER_KEY_ENC_IPV4_DST, &mask->enc_ipv4.dst,
  1587. TCA_FLOWER_KEY_ENC_IPV4_DST_MASK,
  1588. sizeof(key->enc_ipv4.dst))))
  1589. goto nla_put_failure;
  1590. else if (key->enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS &&
  1591. (fl_dump_key_val(skb, &key->enc_ipv6.src,
  1592. TCA_FLOWER_KEY_ENC_IPV6_SRC, &mask->enc_ipv6.src,
  1593. TCA_FLOWER_KEY_ENC_IPV6_SRC_MASK,
  1594. sizeof(key->enc_ipv6.src)) ||
  1595. fl_dump_key_val(skb, &key->enc_ipv6.dst,
  1596. TCA_FLOWER_KEY_ENC_IPV6_DST,
  1597. &mask->enc_ipv6.dst,
  1598. TCA_FLOWER_KEY_ENC_IPV6_DST_MASK,
  1599. sizeof(key->enc_ipv6.dst))))
  1600. goto nla_put_failure;
  1601. if (fl_dump_key_val(skb, &key->enc_key_id, TCA_FLOWER_KEY_ENC_KEY_ID,
  1602. &mask->enc_key_id, TCA_FLOWER_UNSPEC,
  1603. sizeof(key->enc_key_id)) ||
  1604. fl_dump_key_val(skb, &key->enc_tp.src,
  1605. TCA_FLOWER_KEY_ENC_UDP_SRC_PORT,
  1606. &mask->enc_tp.src,
  1607. TCA_FLOWER_KEY_ENC_UDP_SRC_PORT_MASK,
  1608. sizeof(key->enc_tp.src)) ||
  1609. fl_dump_key_val(skb, &key->enc_tp.dst,
  1610. TCA_FLOWER_KEY_ENC_UDP_DST_PORT,
  1611. &mask->enc_tp.dst,
  1612. TCA_FLOWER_KEY_ENC_UDP_DST_PORT_MASK,
  1613. sizeof(key->enc_tp.dst)) ||
  1614. fl_dump_key_ip(skb, true, &key->enc_ip, &mask->enc_ip) ||
  1615. fl_dump_key_enc_opt(skb, &key->enc_opts, &mask->enc_opts))
  1616. goto nla_put_failure;
  1617. if (fl_dump_key_flags(skb, key->control.flags, mask->control.flags))
  1618. goto nla_put_failure;
  1619. return 0;
  1620. nla_put_failure:
  1621. return -EMSGSIZE;
  1622. }
  1623. static int fl_dump(struct net *net, struct tcf_proto *tp, void *fh,
  1624. struct sk_buff *skb, struct tcmsg *t)
  1625. {
  1626. struct cls_fl_filter *f = fh;
  1627. struct nlattr *nest;
  1628. struct fl_flow_key *key, *mask;
  1629. if (!f)
  1630. return skb->len;
  1631. t->tcm_handle = f->handle;
  1632. nest = nla_nest_start(skb, TCA_OPTIONS);
  1633. if (!nest)
  1634. goto nla_put_failure;
  1635. if (f->res.classid &&
  1636. nla_put_u32(skb, TCA_FLOWER_CLASSID, f->res.classid))
  1637. goto nla_put_failure;
  1638. key = &f->key;
  1639. mask = &f->mask->key;
  1640. if (fl_dump_key(skb, net, key, mask))
  1641. goto nla_put_failure;
  1642. if (!tc_skip_hw(f->flags))
  1643. fl_hw_update_stats(tp, f);
  1644. if (f->flags && nla_put_u32(skb, TCA_FLOWER_FLAGS, f->flags))
  1645. goto nla_put_failure;
  1646. if (tcf_exts_dump(skb, &f->exts))
  1647. goto nla_put_failure;
  1648. nla_nest_end(skb, nest);
  1649. if (tcf_exts_dump_stats(skb, &f->exts) < 0)
  1650. goto nla_put_failure;
  1651. return skb->len;
  1652. nla_put_failure:
  1653. nla_nest_cancel(skb, nest);
  1654. return -1;
  1655. }
  1656. static int fl_tmplt_dump(struct sk_buff *skb, struct net *net, void *tmplt_priv)
  1657. {
  1658. struct fl_flow_tmplt *tmplt = tmplt_priv;
  1659. struct fl_flow_key *key, *mask;
  1660. struct nlattr *nest;
  1661. nest = nla_nest_start(skb, TCA_OPTIONS);
  1662. if (!nest)
  1663. goto nla_put_failure;
  1664. key = &tmplt->dummy_key;
  1665. mask = &tmplt->mask;
  1666. if (fl_dump_key(skb, net, key, mask))
  1667. goto nla_put_failure;
  1668. nla_nest_end(skb, nest);
  1669. return skb->len;
  1670. nla_put_failure:
  1671. nla_nest_cancel(skb, nest);
  1672. return -EMSGSIZE;
  1673. }
  1674. static void fl_bind_class(void *fh, u32 classid, unsigned long cl, void *q,
  1675. unsigned long base)
  1676. {
  1677. struct cls_fl_filter *f = fh;
  1678. if (f && f->res.classid == classid) {
  1679. if (cl)
  1680. __tcf_bind_filter(q, &f->res, base);
  1681. else
  1682. __tcf_unbind_filter(q, &f->res);
  1683. }
  1684. }
  1685. static struct tcf_proto_ops cls_fl_ops __read_mostly = {
  1686. .kind = "flower",
  1687. .classify = fl_classify,
  1688. .init = fl_init,
  1689. .destroy = fl_destroy,
  1690. .get = fl_get,
  1691. .change = fl_change,
  1692. .delete = fl_delete,
  1693. .walk = fl_walk,
  1694. .reoffload = fl_reoffload,
  1695. .dump = fl_dump,
  1696. .bind_class = fl_bind_class,
  1697. .tmplt_create = fl_tmplt_create,
  1698. .tmplt_destroy = fl_tmplt_destroy,
  1699. .tmplt_dump = fl_tmplt_dump,
  1700. .owner = THIS_MODULE,
  1701. };
  1702. static int __init cls_fl_init(void)
  1703. {
  1704. return register_tcf_proto_ops(&cls_fl_ops);
  1705. }
  1706. static void __exit cls_fl_exit(void)
  1707. {
  1708. unregister_tcf_proto_ops(&cls_fl_ops);
  1709. }
  1710. module_init(cls_fl_init);
  1711. module_exit(cls_fl_exit);
  1712. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  1713. MODULE_DESCRIPTION("Flower classifier");
  1714. MODULE_LICENSE("GPL v2");