flow_dissector.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. #include <linux/kernel.h>
  2. #include <linux/skbuff.h>
  3. #include <linux/export.h>
  4. #include <linux/ip.h>
  5. #include <linux/ipv6.h>
  6. #include <linux/if_vlan.h>
  7. #include <net/ip.h>
  8. #include <net/ipv6.h>
  9. #include <linux/igmp.h>
  10. #include <linux/icmp.h>
  11. #include <linux/sctp.h>
  12. #include <linux/dccp.h>
  13. #include <linux/if_tunnel.h>
  14. #include <linux/if_pppox.h>
  15. #include <linux/ppp_defs.h>
  16. #include <linux/stddef.h>
  17. #include <linux/if_ether.h>
  18. #include <linux/mpls.h>
  19. #include <net/flow_dissector.h>
  20. #include <scsi/fc/fc_fcoe.h>
  21. static bool dissector_uses_key(const struct flow_dissector *flow_dissector,
  22. enum flow_dissector_key_id key_id)
  23. {
  24. return flow_dissector->used_keys & (1 << key_id);
  25. }
  26. static void dissector_set_key(struct flow_dissector *flow_dissector,
  27. enum flow_dissector_key_id key_id)
  28. {
  29. flow_dissector->used_keys |= (1 << key_id);
  30. }
  31. static void *skb_flow_dissector_target(struct flow_dissector *flow_dissector,
  32. enum flow_dissector_key_id key_id,
  33. void *target_container)
  34. {
  35. return ((char *) target_container) + flow_dissector->offset[key_id];
  36. }
  37. void skb_flow_dissector_init(struct flow_dissector *flow_dissector,
  38. const struct flow_dissector_key *key,
  39. unsigned int key_count)
  40. {
  41. unsigned int i;
  42. memset(flow_dissector, 0, sizeof(*flow_dissector));
  43. for (i = 0; i < key_count; i++, key++) {
  44. /* User should make sure that every key target offset is withing
  45. * boundaries of unsigned short.
  46. */
  47. BUG_ON(key->offset > USHRT_MAX);
  48. BUG_ON(dissector_uses_key(flow_dissector,
  49. key->key_id));
  50. dissector_set_key(flow_dissector, key->key_id);
  51. flow_dissector->offset[key->key_id] = key->offset;
  52. }
  53. /* Ensure that the dissector always includes control and basic key.
  54. * That way we are able to avoid handling lack of these in fast path.
  55. */
  56. BUG_ON(!dissector_uses_key(flow_dissector,
  57. FLOW_DISSECTOR_KEY_CONTROL));
  58. BUG_ON(!dissector_uses_key(flow_dissector,
  59. FLOW_DISSECTOR_KEY_BASIC));
  60. }
  61. EXPORT_SYMBOL(skb_flow_dissector_init);
  62. /**
  63. * __skb_flow_get_ports - extract the upper layer ports and return them
  64. * @skb: sk_buff to extract the ports from
  65. * @thoff: transport header offset
  66. * @ip_proto: protocol for which to get port offset
  67. * @data: raw buffer pointer to the packet, if NULL use skb->data
  68. * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
  69. *
  70. * The function will try to retrieve the ports at offset thoff + poff where poff
  71. * is the protocol port offset returned from proto_ports_offset
  72. */
  73. __be32 __skb_flow_get_ports(const struct sk_buff *skb, int thoff, u8 ip_proto,
  74. void *data, int hlen)
  75. {
  76. int poff = proto_ports_offset(ip_proto);
  77. if (!data) {
  78. data = skb->data;
  79. hlen = skb_headlen(skb);
  80. }
  81. if (poff >= 0) {
  82. __be32 *ports, _ports;
  83. ports = __skb_header_pointer(skb, thoff + poff,
  84. sizeof(_ports), data, hlen, &_ports);
  85. if (ports)
  86. return *ports;
  87. }
  88. return 0;
  89. }
  90. EXPORT_SYMBOL(__skb_flow_get_ports);
  91. /**
  92. * __skb_flow_dissect - extract the flow_keys struct and return it
  93. * @skb: sk_buff to extract the flow from, can be NULL if the rest are specified
  94. * @flow_dissector: list of keys to dissect
  95. * @target_container: target structure to put dissected values into
  96. * @data: raw buffer pointer to the packet, if NULL use skb->data
  97. * @proto: protocol for which to get the flow, if @data is NULL use skb->protocol
  98. * @nhoff: network header offset, if @data is NULL use skb_network_offset(skb)
  99. * @hlen: packet header length, if @data is NULL use skb_headlen(skb)
  100. *
  101. * The function will try to retrieve individual keys into target specified
  102. * by flow_dissector from either the skbuff or a raw buffer specified by the
  103. * rest parameters.
  104. *
  105. * Caller must take care of zeroing target container memory.
  106. */
  107. bool __skb_flow_dissect(const struct sk_buff *skb,
  108. struct flow_dissector *flow_dissector,
  109. void *target_container,
  110. void *data, __be16 proto, int nhoff, int hlen,
  111. unsigned int flags)
  112. {
  113. struct flow_dissector_key_control *key_control;
  114. struct flow_dissector_key_basic *key_basic;
  115. struct flow_dissector_key_addrs *key_addrs;
  116. struct flow_dissector_key_ports *key_ports;
  117. struct flow_dissector_key_tags *key_tags;
  118. struct flow_dissector_key_keyid *key_keyid;
  119. u8 ip_proto = 0;
  120. bool ret;
  121. if (!data) {
  122. data = skb->data;
  123. proto = skb->protocol;
  124. nhoff = skb_network_offset(skb);
  125. hlen = skb_headlen(skb);
  126. }
  127. /* It is ensured by skb_flow_dissector_init() that control key will
  128. * be always present.
  129. */
  130. key_control = skb_flow_dissector_target(flow_dissector,
  131. FLOW_DISSECTOR_KEY_CONTROL,
  132. target_container);
  133. /* It is ensured by skb_flow_dissector_init() that basic key will
  134. * be always present.
  135. */
  136. key_basic = skb_flow_dissector_target(flow_dissector,
  137. FLOW_DISSECTOR_KEY_BASIC,
  138. target_container);
  139. if (dissector_uses_key(flow_dissector,
  140. FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  141. struct ethhdr *eth = eth_hdr(skb);
  142. struct flow_dissector_key_eth_addrs *key_eth_addrs;
  143. key_eth_addrs = skb_flow_dissector_target(flow_dissector,
  144. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  145. target_container);
  146. memcpy(key_eth_addrs, &eth->h_dest, sizeof(*key_eth_addrs));
  147. }
  148. again:
  149. switch (proto) {
  150. case htons(ETH_P_IP): {
  151. const struct iphdr *iph;
  152. struct iphdr _iph;
  153. ip:
  154. iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
  155. if (!iph || iph->ihl < 5)
  156. goto out_bad;
  157. nhoff += iph->ihl * 4;
  158. ip_proto = iph->protocol;
  159. if (!dissector_uses_key(flow_dissector,
  160. FLOW_DISSECTOR_KEY_IPV4_ADDRS))
  161. break;
  162. key_addrs = skb_flow_dissector_target(flow_dissector,
  163. FLOW_DISSECTOR_KEY_IPV4_ADDRS, target_container);
  164. memcpy(&key_addrs->v4addrs, &iph->saddr,
  165. sizeof(key_addrs->v4addrs));
  166. key_control->addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  167. if (ip_is_fragment(iph)) {
  168. key_control->flags |= FLOW_DIS_IS_FRAGMENT;
  169. if (iph->frag_off & htons(IP_OFFSET)) {
  170. goto out_good;
  171. } else {
  172. key_control->flags |= FLOW_DIS_FIRST_FRAG;
  173. if (!(flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG))
  174. goto out_good;
  175. }
  176. }
  177. if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
  178. goto out_good;
  179. break;
  180. }
  181. case htons(ETH_P_IPV6): {
  182. const struct ipv6hdr *iph;
  183. struct ipv6hdr _iph;
  184. ipv6:
  185. iph = __skb_header_pointer(skb, nhoff, sizeof(_iph), data, hlen, &_iph);
  186. if (!iph)
  187. goto out_bad;
  188. ip_proto = iph->nexthdr;
  189. nhoff += sizeof(struct ipv6hdr);
  190. if (dissector_uses_key(flow_dissector,
  191. FLOW_DISSECTOR_KEY_IPV6_ADDRS)) {
  192. struct flow_dissector_key_ipv6_addrs *key_ipv6_addrs;
  193. key_ipv6_addrs = skb_flow_dissector_target(flow_dissector,
  194. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  195. target_container);
  196. memcpy(key_ipv6_addrs, &iph->saddr, sizeof(*key_ipv6_addrs));
  197. key_control->addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  198. }
  199. if ((dissector_uses_key(flow_dissector,
  200. FLOW_DISSECTOR_KEY_FLOW_LABEL) ||
  201. (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)) &&
  202. ip6_flowlabel(iph)) {
  203. __be32 flow_label = ip6_flowlabel(iph);
  204. if (dissector_uses_key(flow_dissector,
  205. FLOW_DISSECTOR_KEY_FLOW_LABEL)) {
  206. key_tags = skb_flow_dissector_target(flow_dissector,
  207. FLOW_DISSECTOR_KEY_FLOW_LABEL,
  208. target_container);
  209. key_tags->flow_label = ntohl(flow_label);
  210. }
  211. if (flags & FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL)
  212. goto out_good;
  213. }
  214. if (flags & FLOW_DISSECTOR_F_STOP_AT_L3)
  215. goto out_good;
  216. break;
  217. }
  218. case htons(ETH_P_8021AD):
  219. case htons(ETH_P_8021Q): {
  220. const struct vlan_hdr *vlan;
  221. struct vlan_hdr _vlan;
  222. vlan = __skb_header_pointer(skb, nhoff, sizeof(_vlan), data, hlen, &_vlan);
  223. if (!vlan)
  224. goto out_bad;
  225. if (dissector_uses_key(flow_dissector,
  226. FLOW_DISSECTOR_KEY_VLANID)) {
  227. key_tags = skb_flow_dissector_target(flow_dissector,
  228. FLOW_DISSECTOR_KEY_VLANID,
  229. target_container);
  230. key_tags->vlan_id = skb_vlan_tag_get_id(skb);
  231. }
  232. proto = vlan->h_vlan_encapsulated_proto;
  233. nhoff += sizeof(*vlan);
  234. goto again;
  235. }
  236. case htons(ETH_P_PPP_SES): {
  237. struct {
  238. struct pppoe_hdr hdr;
  239. __be16 proto;
  240. } *hdr, _hdr;
  241. hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
  242. if (!hdr)
  243. goto out_bad;
  244. proto = hdr->proto;
  245. nhoff += PPPOE_SES_HLEN;
  246. switch (proto) {
  247. case htons(PPP_IP):
  248. goto ip;
  249. case htons(PPP_IPV6):
  250. goto ipv6;
  251. default:
  252. goto out_bad;
  253. }
  254. }
  255. case htons(ETH_P_TIPC): {
  256. struct {
  257. __be32 pre[3];
  258. __be32 srcnode;
  259. } *hdr, _hdr;
  260. hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
  261. if (!hdr)
  262. goto out_bad;
  263. if (dissector_uses_key(flow_dissector,
  264. FLOW_DISSECTOR_KEY_TIPC_ADDRS)) {
  265. key_addrs = skb_flow_dissector_target(flow_dissector,
  266. FLOW_DISSECTOR_KEY_TIPC_ADDRS,
  267. target_container);
  268. key_addrs->tipcaddrs.srcnode = hdr->srcnode;
  269. key_control->addr_type = FLOW_DISSECTOR_KEY_TIPC_ADDRS;
  270. }
  271. goto out_good;
  272. }
  273. case htons(ETH_P_MPLS_UC):
  274. case htons(ETH_P_MPLS_MC): {
  275. struct mpls_label *hdr, _hdr[2];
  276. mpls:
  277. hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data,
  278. hlen, &_hdr);
  279. if (!hdr)
  280. goto out_bad;
  281. if ((ntohl(hdr[0].entry) & MPLS_LS_LABEL_MASK) >>
  282. MPLS_LS_LABEL_SHIFT == MPLS_LABEL_ENTROPY) {
  283. if (dissector_uses_key(flow_dissector,
  284. FLOW_DISSECTOR_KEY_MPLS_ENTROPY)) {
  285. key_keyid = skb_flow_dissector_target(flow_dissector,
  286. FLOW_DISSECTOR_KEY_MPLS_ENTROPY,
  287. target_container);
  288. key_keyid->keyid = hdr[1].entry &
  289. htonl(MPLS_LS_LABEL_MASK);
  290. }
  291. goto out_good;
  292. }
  293. goto out_good;
  294. }
  295. case htons(ETH_P_FCOE):
  296. key_control->thoff = (u16)(nhoff + FCOE_HEADER_LEN);
  297. /* fall through */
  298. default:
  299. goto out_bad;
  300. }
  301. ip_proto_again:
  302. switch (ip_proto) {
  303. case IPPROTO_GRE: {
  304. struct gre_hdr {
  305. __be16 flags;
  306. __be16 proto;
  307. } *hdr, _hdr;
  308. hdr = __skb_header_pointer(skb, nhoff, sizeof(_hdr), data, hlen, &_hdr);
  309. if (!hdr)
  310. goto out_bad;
  311. /*
  312. * Only look inside GRE if version zero and no
  313. * routing
  314. */
  315. if (hdr->flags & (GRE_VERSION | GRE_ROUTING))
  316. break;
  317. proto = hdr->proto;
  318. nhoff += 4;
  319. if (hdr->flags & GRE_CSUM)
  320. nhoff += 4;
  321. if (hdr->flags & GRE_KEY) {
  322. const __be32 *keyid;
  323. __be32 _keyid;
  324. keyid = __skb_header_pointer(skb, nhoff, sizeof(_keyid),
  325. data, hlen, &_keyid);
  326. if (!keyid)
  327. goto out_bad;
  328. if (dissector_uses_key(flow_dissector,
  329. FLOW_DISSECTOR_KEY_GRE_KEYID)) {
  330. key_keyid = skb_flow_dissector_target(flow_dissector,
  331. FLOW_DISSECTOR_KEY_GRE_KEYID,
  332. target_container);
  333. key_keyid->keyid = *keyid;
  334. }
  335. nhoff += 4;
  336. }
  337. if (hdr->flags & GRE_SEQ)
  338. nhoff += 4;
  339. if (proto == htons(ETH_P_TEB)) {
  340. const struct ethhdr *eth;
  341. struct ethhdr _eth;
  342. eth = __skb_header_pointer(skb, nhoff,
  343. sizeof(_eth),
  344. data, hlen, &_eth);
  345. if (!eth)
  346. goto out_bad;
  347. proto = eth->h_proto;
  348. nhoff += sizeof(*eth);
  349. /* Cap headers that we access via pointers at the
  350. * end of the Ethernet header as our maximum alignment
  351. * at that point is only 2 bytes.
  352. */
  353. if (NET_IP_ALIGN)
  354. hlen = nhoff;
  355. }
  356. key_control->flags |= FLOW_DIS_ENCAPSULATION;
  357. if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
  358. goto out_good;
  359. goto again;
  360. }
  361. case NEXTHDR_HOP:
  362. case NEXTHDR_ROUTING:
  363. case NEXTHDR_DEST: {
  364. u8 _opthdr[2], *opthdr;
  365. if (proto != htons(ETH_P_IPV6))
  366. break;
  367. opthdr = __skb_header_pointer(skb, nhoff, sizeof(_opthdr),
  368. data, hlen, &_opthdr);
  369. if (!opthdr)
  370. goto out_bad;
  371. ip_proto = opthdr[0];
  372. nhoff += (opthdr[1] + 1) << 3;
  373. goto ip_proto_again;
  374. }
  375. case NEXTHDR_FRAGMENT: {
  376. struct frag_hdr _fh, *fh;
  377. if (proto != htons(ETH_P_IPV6))
  378. break;
  379. fh = __skb_header_pointer(skb, nhoff, sizeof(_fh),
  380. data, hlen, &_fh);
  381. if (!fh)
  382. goto out_bad;
  383. key_control->flags |= FLOW_DIS_IS_FRAGMENT;
  384. nhoff += sizeof(_fh);
  385. if (!(fh->frag_off & htons(IP6_OFFSET))) {
  386. key_control->flags |= FLOW_DIS_FIRST_FRAG;
  387. if (flags & FLOW_DISSECTOR_F_PARSE_1ST_FRAG) {
  388. ip_proto = fh->nexthdr;
  389. goto ip_proto_again;
  390. }
  391. }
  392. goto out_good;
  393. }
  394. case IPPROTO_IPIP:
  395. proto = htons(ETH_P_IP);
  396. key_control->flags |= FLOW_DIS_ENCAPSULATION;
  397. if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
  398. goto out_good;
  399. goto ip;
  400. case IPPROTO_IPV6:
  401. proto = htons(ETH_P_IPV6);
  402. key_control->flags |= FLOW_DIS_ENCAPSULATION;
  403. if (flags & FLOW_DISSECTOR_F_STOP_AT_ENCAP)
  404. goto out_good;
  405. goto ipv6;
  406. case IPPROTO_MPLS:
  407. proto = htons(ETH_P_MPLS_UC);
  408. goto mpls;
  409. default:
  410. break;
  411. }
  412. if (dissector_uses_key(flow_dissector, FLOW_DISSECTOR_KEY_PORTS) &&
  413. !(key_control->flags & FLOW_DIS_IS_FRAGMENT)) {
  414. key_ports = skb_flow_dissector_target(flow_dissector,
  415. FLOW_DISSECTOR_KEY_PORTS,
  416. target_container);
  417. key_ports->ports = __skb_flow_get_ports(skb, nhoff, ip_proto,
  418. data, hlen);
  419. }
  420. out_good:
  421. ret = true;
  422. out:
  423. key_control->thoff = min_t(u16, nhoff, skb ? skb->len : hlen);
  424. key_basic->n_proto = proto;
  425. key_basic->ip_proto = ip_proto;
  426. return ret;
  427. out_bad:
  428. ret = false;
  429. goto out;
  430. }
  431. EXPORT_SYMBOL(__skb_flow_dissect);
  432. static u32 hashrnd __read_mostly;
  433. static __always_inline void __flow_hash_secret_init(void)
  434. {
  435. net_get_random_once(&hashrnd, sizeof(hashrnd));
  436. }
  437. static __always_inline u32 __flow_hash_words(const u32 *words, u32 length,
  438. u32 keyval)
  439. {
  440. return jhash2(words, length, keyval);
  441. }
  442. static inline const u32 *flow_keys_hash_start(const struct flow_keys *flow)
  443. {
  444. const void *p = flow;
  445. BUILD_BUG_ON(FLOW_KEYS_HASH_OFFSET % sizeof(u32));
  446. return (const u32 *)(p + FLOW_KEYS_HASH_OFFSET);
  447. }
  448. static inline size_t flow_keys_hash_length(const struct flow_keys *flow)
  449. {
  450. size_t diff = FLOW_KEYS_HASH_OFFSET + sizeof(flow->addrs);
  451. BUILD_BUG_ON((sizeof(*flow) - FLOW_KEYS_HASH_OFFSET) % sizeof(u32));
  452. BUILD_BUG_ON(offsetof(typeof(*flow), addrs) !=
  453. sizeof(*flow) - sizeof(flow->addrs));
  454. switch (flow->control.addr_type) {
  455. case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
  456. diff -= sizeof(flow->addrs.v4addrs);
  457. break;
  458. case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
  459. diff -= sizeof(flow->addrs.v6addrs);
  460. break;
  461. case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
  462. diff -= sizeof(flow->addrs.tipcaddrs);
  463. break;
  464. }
  465. return (sizeof(*flow) - diff) / sizeof(u32);
  466. }
  467. __be32 flow_get_u32_src(const struct flow_keys *flow)
  468. {
  469. switch (flow->control.addr_type) {
  470. case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
  471. return flow->addrs.v4addrs.src;
  472. case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
  473. return (__force __be32)ipv6_addr_hash(
  474. &flow->addrs.v6addrs.src);
  475. case FLOW_DISSECTOR_KEY_TIPC_ADDRS:
  476. return flow->addrs.tipcaddrs.srcnode;
  477. default:
  478. return 0;
  479. }
  480. }
  481. EXPORT_SYMBOL(flow_get_u32_src);
  482. __be32 flow_get_u32_dst(const struct flow_keys *flow)
  483. {
  484. switch (flow->control.addr_type) {
  485. case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
  486. return flow->addrs.v4addrs.dst;
  487. case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
  488. return (__force __be32)ipv6_addr_hash(
  489. &flow->addrs.v6addrs.dst);
  490. default:
  491. return 0;
  492. }
  493. }
  494. EXPORT_SYMBOL(flow_get_u32_dst);
  495. static inline void __flow_hash_consistentify(struct flow_keys *keys)
  496. {
  497. int addr_diff, i;
  498. switch (keys->control.addr_type) {
  499. case FLOW_DISSECTOR_KEY_IPV4_ADDRS:
  500. addr_diff = (__force u32)keys->addrs.v4addrs.dst -
  501. (__force u32)keys->addrs.v4addrs.src;
  502. if ((addr_diff < 0) ||
  503. (addr_diff == 0 &&
  504. ((__force u16)keys->ports.dst <
  505. (__force u16)keys->ports.src))) {
  506. swap(keys->addrs.v4addrs.src, keys->addrs.v4addrs.dst);
  507. swap(keys->ports.src, keys->ports.dst);
  508. }
  509. break;
  510. case FLOW_DISSECTOR_KEY_IPV6_ADDRS:
  511. addr_diff = memcmp(&keys->addrs.v6addrs.dst,
  512. &keys->addrs.v6addrs.src,
  513. sizeof(keys->addrs.v6addrs.dst));
  514. if ((addr_diff < 0) ||
  515. (addr_diff == 0 &&
  516. ((__force u16)keys->ports.dst <
  517. (__force u16)keys->ports.src))) {
  518. for (i = 0; i < 4; i++)
  519. swap(keys->addrs.v6addrs.src.s6_addr32[i],
  520. keys->addrs.v6addrs.dst.s6_addr32[i]);
  521. swap(keys->ports.src, keys->ports.dst);
  522. }
  523. break;
  524. }
  525. }
  526. static inline u32 __flow_hash_from_keys(struct flow_keys *keys, u32 keyval)
  527. {
  528. u32 hash;
  529. __flow_hash_consistentify(keys);
  530. hash = __flow_hash_words(flow_keys_hash_start(keys),
  531. flow_keys_hash_length(keys), keyval);
  532. if (!hash)
  533. hash = 1;
  534. return hash;
  535. }
  536. u32 flow_hash_from_keys(struct flow_keys *keys)
  537. {
  538. __flow_hash_secret_init();
  539. return __flow_hash_from_keys(keys, hashrnd);
  540. }
  541. EXPORT_SYMBOL(flow_hash_from_keys);
  542. static inline u32 ___skb_get_hash(const struct sk_buff *skb,
  543. struct flow_keys *keys, u32 keyval)
  544. {
  545. skb_flow_dissect_flow_keys(skb, keys,
  546. FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
  547. return __flow_hash_from_keys(keys, keyval);
  548. }
  549. struct _flow_keys_digest_data {
  550. __be16 n_proto;
  551. u8 ip_proto;
  552. u8 padding;
  553. __be32 ports;
  554. __be32 src;
  555. __be32 dst;
  556. };
  557. void make_flow_keys_digest(struct flow_keys_digest *digest,
  558. const struct flow_keys *flow)
  559. {
  560. struct _flow_keys_digest_data *data =
  561. (struct _flow_keys_digest_data *)digest;
  562. BUILD_BUG_ON(sizeof(*data) > sizeof(*digest));
  563. memset(digest, 0, sizeof(*digest));
  564. data->n_proto = flow->basic.n_proto;
  565. data->ip_proto = flow->basic.ip_proto;
  566. data->ports = flow->ports.ports;
  567. data->src = flow->addrs.v4addrs.src;
  568. data->dst = flow->addrs.v4addrs.dst;
  569. }
  570. EXPORT_SYMBOL(make_flow_keys_digest);
  571. static struct flow_dissector flow_keys_dissector_symmetric __read_mostly;
  572. u32 __skb_get_hash_symmetric(struct sk_buff *skb)
  573. {
  574. struct flow_keys keys;
  575. __flow_hash_secret_init();
  576. memset(&keys, 0, sizeof(keys));
  577. __skb_flow_dissect(skb, &flow_keys_dissector_symmetric, &keys,
  578. NULL, 0, 0, 0,
  579. FLOW_DISSECTOR_F_STOP_AT_FLOW_LABEL);
  580. return __flow_hash_from_keys(&keys, hashrnd);
  581. }
  582. EXPORT_SYMBOL_GPL(__skb_get_hash_symmetric);
  583. /**
  584. * __skb_get_hash: calculate a flow hash
  585. * @skb: sk_buff to calculate flow hash from
  586. *
  587. * This function calculates a flow hash based on src/dst addresses
  588. * and src/dst port numbers. Sets hash in skb to non-zero hash value
  589. * on success, zero indicates no valid hash. Also, sets l4_hash in skb
  590. * if hash is a canonical 4-tuple hash over transport ports.
  591. */
  592. void __skb_get_hash(struct sk_buff *skb)
  593. {
  594. struct flow_keys keys;
  595. __flow_hash_secret_init();
  596. __skb_set_sw_hash(skb, ___skb_get_hash(skb, &keys, hashrnd),
  597. flow_keys_have_l4(&keys));
  598. }
  599. EXPORT_SYMBOL(__skb_get_hash);
  600. __u32 skb_get_hash_perturb(const struct sk_buff *skb, u32 perturb)
  601. {
  602. struct flow_keys keys;
  603. return ___skb_get_hash(skb, &keys, perturb);
  604. }
  605. EXPORT_SYMBOL(skb_get_hash_perturb);
  606. __u32 __skb_get_hash_flowi6(struct sk_buff *skb, const struct flowi6 *fl6)
  607. {
  608. struct flow_keys keys;
  609. memset(&keys, 0, sizeof(keys));
  610. memcpy(&keys.addrs.v6addrs.src, &fl6->saddr,
  611. sizeof(keys.addrs.v6addrs.src));
  612. memcpy(&keys.addrs.v6addrs.dst, &fl6->daddr,
  613. sizeof(keys.addrs.v6addrs.dst));
  614. keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  615. keys.ports.src = fl6->fl6_sport;
  616. keys.ports.dst = fl6->fl6_dport;
  617. keys.keyid.keyid = fl6->fl6_gre_key;
  618. keys.tags.flow_label = (__force u32)fl6->flowlabel;
  619. keys.basic.ip_proto = fl6->flowi6_proto;
  620. __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
  621. flow_keys_have_l4(&keys));
  622. return skb->hash;
  623. }
  624. EXPORT_SYMBOL(__skb_get_hash_flowi6);
  625. __u32 __skb_get_hash_flowi4(struct sk_buff *skb, const struct flowi4 *fl4)
  626. {
  627. struct flow_keys keys;
  628. memset(&keys, 0, sizeof(keys));
  629. keys.addrs.v4addrs.src = fl4->saddr;
  630. keys.addrs.v4addrs.dst = fl4->daddr;
  631. keys.control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  632. keys.ports.src = fl4->fl4_sport;
  633. keys.ports.dst = fl4->fl4_dport;
  634. keys.keyid.keyid = fl4->fl4_gre_key;
  635. keys.basic.ip_proto = fl4->flowi4_proto;
  636. __skb_set_sw_hash(skb, flow_hash_from_keys(&keys),
  637. flow_keys_have_l4(&keys));
  638. return skb->hash;
  639. }
  640. EXPORT_SYMBOL(__skb_get_hash_flowi4);
  641. u32 __skb_get_poff(const struct sk_buff *skb, void *data,
  642. const struct flow_keys *keys, int hlen)
  643. {
  644. u32 poff = keys->control.thoff;
  645. switch (keys->basic.ip_proto) {
  646. case IPPROTO_TCP: {
  647. /* access doff as u8 to avoid unaligned access */
  648. const u8 *doff;
  649. u8 _doff;
  650. doff = __skb_header_pointer(skb, poff + 12, sizeof(_doff),
  651. data, hlen, &_doff);
  652. if (!doff)
  653. return poff;
  654. poff += max_t(u32, sizeof(struct tcphdr), (*doff & 0xF0) >> 2);
  655. break;
  656. }
  657. case IPPROTO_UDP:
  658. case IPPROTO_UDPLITE:
  659. poff += sizeof(struct udphdr);
  660. break;
  661. /* For the rest, we do not really care about header
  662. * extensions at this point for now.
  663. */
  664. case IPPROTO_ICMP:
  665. poff += sizeof(struct icmphdr);
  666. break;
  667. case IPPROTO_ICMPV6:
  668. poff += sizeof(struct icmp6hdr);
  669. break;
  670. case IPPROTO_IGMP:
  671. poff += sizeof(struct igmphdr);
  672. break;
  673. case IPPROTO_DCCP:
  674. poff += sizeof(struct dccp_hdr);
  675. break;
  676. case IPPROTO_SCTP:
  677. poff += sizeof(struct sctphdr);
  678. break;
  679. }
  680. return poff;
  681. }
  682. /**
  683. * skb_get_poff - get the offset to the payload
  684. * @skb: sk_buff to get the payload offset from
  685. *
  686. * The function will get the offset to the payload as far as it could
  687. * be dissected. The main user is currently BPF, so that we can dynamically
  688. * truncate packets without needing to push actual payload to the user
  689. * space and can analyze headers only, instead.
  690. */
  691. u32 skb_get_poff(const struct sk_buff *skb)
  692. {
  693. struct flow_keys keys;
  694. if (!skb_flow_dissect_flow_keys(skb, &keys, 0))
  695. return 0;
  696. return __skb_get_poff(skb, skb->data, &keys, skb_headlen(skb));
  697. }
  698. __u32 __get_hash_from_flowi6(const struct flowi6 *fl6, struct flow_keys *keys)
  699. {
  700. memset(keys, 0, sizeof(*keys));
  701. memcpy(&keys->addrs.v6addrs.src, &fl6->saddr,
  702. sizeof(keys->addrs.v6addrs.src));
  703. memcpy(&keys->addrs.v6addrs.dst, &fl6->daddr,
  704. sizeof(keys->addrs.v6addrs.dst));
  705. keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV6_ADDRS;
  706. keys->ports.src = fl6->fl6_sport;
  707. keys->ports.dst = fl6->fl6_dport;
  708. keys->keyid.keyid = fl6->fl6_gre_key;
  709. keys->tags.flow_label = (__force u32)fl6->flowlabel;
  710. keys->basic.ip_proto = fl6->flowi6_proto;
  711. return flow_hash_from_keys(keys);
  712. }
  713. EXPORT_SYMBOL(__get_hash_from_flowi6);
  714. __u32 __get_hash_from_flowi4(const struct flowi4 *fl4, struct flow_keys *keys)
  715. {
  716. memset(keys, 0, sizeof(*keys));
  717. keys->addrs.v4addrs.src = fl4->saddr;
  718. keys->addrs.v4addrs.dst = fl4->daddr;
  719. keys->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  720. keys->ports.src = fl4->fl4_sport;
  721. keys->ports.dst = fl4->fl4_dport;
  722. keys->keyid.keyid = fl4->fl4_gre_key;
  723. keys->basic.ip_proto = fl4->flowi4_proto;
  724. return flow_hash_from_keys(keys);
  725. }
  726. EXPORT_SYMBOL(__get_hash_from_flowi4);
  727. static const struct flow_dissector_key flow_keys_dissector_keys[] = {
  728. {
  729. .key_id = FLOW_DISSECTOR_KEY_CONTROL,
  730. .offset = offsetof(struct flow_keys, control),
  731. },
  732. {
  733. .key_id = FLOW_DISSECTOR_KEY_BASIC,
  734. .offset = offsetof(struct flow_keys, basic),
  735. },
  736. {
  737. .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  738. .offset = offsetof(struct flow_keys, addrs.v4addrs),
  739. },
  740. {
  741. .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  742. .offset = offsetof(struct flow_keys, addrs.v6addrs),
  743. },
  744. {
  745. .key_id = FLOW_DISSECTOR_KEY_TIPC_ADDRS,
  746. .offset = offsetof(struct flow_keys, addrs.tipcaddrs),
  747. },
  748. {
  749. .key_id = FLOW_DISSECTOR_KEY_PORTS,
  750. .offset = offsetof(struct flow_keys, ports),
  751. },
  752. {
  753. .key_id = FLOW_DISSECTOR_KEY_VLANID,
  754. .offset = offsetof(struct flow_keys, tags),
  755. },
  756. {
  757. .key_id = FLOW_DISSECTOR_KEY_FLOW_LABEL,
  758. .offset = offsetof(struct flow_keys, tags),
  759. },
  760. {
  761. .key_id = FLOW_DISSECTOR_KEY_GRE_KEYID,
  762. .offset = offsetof(struct flow_keys, keyid),
  763. },
  764. };
  765. static const struct flow_dissector_key flow_keys_dissector_symmetric_keys[] = {
  766. {
  767. .key_id = FLOW_DISSECTOR_KEY_CONTROL,
  768. .offset = offsetof(struct flow_keys, control),
  769. },
  770. {
  771. .key_id = FLOW_DISSECTOR_KEY_BASIC,
  772. .offset = offsetof(struct flow_keys, basic),
  773. },
  774. {
  775. .key_id = FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  776. .offset = offsetof(struct flow_keys, addrs.v4addrs),
  777. },
  778. {
  779. .key_id = FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  780. .offset = offsetof(struct flow_keys, addrs.v6addrs),
  781. },
  782. {
  783. .key_id = FLOW_DISSECTOR_KEY_PORTS,
  784. .offset = offsetof(struct flow_keys, ports),
  785. },
  786. };
  787. static const struct flow_dissector_key flow_keys_buf_dissector_keys[] = {
  788. {
  789. .key_id = FLOW_DISSECTOR_KEY_CONTROL,
  790. .offset = offsetof(struct flow_keys, control),
  791. },
  792. {
  793. .key_id = FLOW_DISSECTOR_KEY_BASIC,
  794. .offset = offsetof(struct flow_keys, basic),
  795. },
  796. };
  797. struct flow_dissector flow_keys_dissector __read_mostly;
  798. EXPORT_SYMBOL(flow_keys_dissector);
  799. struct flow_dissector flow_keys_buf_dissector __read_mostly;
  800. static int __init init_default_flow_dissectors(void)
  801. {
  802. skb_flow_dissector_init(&flow_keys_dissector,
  803. flow_keys_dissector_keys,
  804. ARRAY_SIZE(flow_keys_dissector_keys));
  805. skb_flow_dissector_init(&flow_keys_dissector_symmetric,
  806. flow_keys_dissector_symmetric_keys,
  807. ARRAY_SIZE(flow_keys_dissector_symmetric_keys));
  808. skb_flow_dissector_init(&flow_keys_buf_dissector,
  809. flow_keys_buf_dissector_keys,
  810. ARRAY_SIZE(flow_keys_buf_dissector_keys));
  811. return 0;
  812. }
  813. core_initcall(init_default_flow_dissectors);