flow_dissector.c 27 KB

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