flow_dissector.c 26 KB

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