blob: 1873ef64260515fd2d8254c341cba8a7addee267 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001/* SPDX-License-Identifier: GPL-2.0-or-later */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * INET An implementation of the TCP/IP protocol suite for the LINUX
4 * operating system. INET is implemented using the BSD Socket
5 * interface as the means of communication with the user level.
6 *
7 * Definitions of the Internet Protocol.
8 *
9 * Version: @(#)in.h 1.0.1 04/21/93
10 *
11 * Authors: Original taken from the GNU Project <netinet/in.h> file.
12 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14#ifndef _LINUX_IN_H
15#define _LINUX_IN_H
16
Joe Perches2658fa82007-12-16 13:42:49 -080017
Changli Gaoe7607022010-08-17 19:03:44 +000018#include <linux/errno.h>
David Howells607ca462012-10-13 10:46:48 +010019#include <uapi/linux/in.h>
Changli Gaoe7607022010-08-17 19:03:44 +000020
21static inline int proto_ports_offset(int proto)
22{
23 switch (proto) {
24 case IPPROTO_TCP:
25 case IPPROTO_UDP:
26 case IPPROTO_DCCP:
27 case IPPROTO_ESP: /* SPI */
28 case IPPROTO_SCTP:
29 case IPPROTO_UDPLITE:
30 return 0;
31 case IPPROTO_AH: /* SPI */
32 return 4;
33 default:
34 return -EINVAL;
35 }
36}
37
Joe Perches2658fa82007-12-16 13:42:49 -080038static inline bool ipv4_is_loopback(__be32 addr)
39{
40 return (addr & htonl(0xff000000)) == htonl(0x7f000000);
41}
42
43static inline bool ipv4_is_multicast(__be32 addr)
44{
45 return (addr & htonl(0xf0000000)) == htonl(0xe0000000);
46}
47
48static inline bool ipv4_is_local_multicast(__be32 addr)
49{
50 return (addr & htonl(0xffffff00)) == htonl(0xe0000000);
51}
52
Jan Engelhardt1e637c72008-01-21 03:18:08 -080053static inline bool ipv4_is_lbcast(__be32 addr)
Joe Perches2658fa82007-12-16 13:42:49 -080054{
Jan Engelhardt1e637c72008-01-21 03:18:08 -080055 /* limited broadcast */
Al Viro0ff96632008-03-17 22:48:46 -070056 return addr == htonl(INADDR_BROADCAST);
Joe Perches2658fa82007-12-16 13:42:49 -080057}
58
Linus Lüssing4b3087c2019-01-21 07:26:28 +010059static inline bool ipv4_is_all_snoopers(__be32 addr)
60{
61 return addr == htonl(INADDR_ALLSNOOPERS_GROUP);
62}
63
Joe Perches2658fa82007-12-16 13:42:49 -080064static inline bool ipv4_is_zeronet(__be32 addr)
65{
Dave Taht96125bf2019-06-22 10:07:34 -070066 return (addr == 0);
Joe Perches2658fa82007-12-16 13:42:49 -080067}
68
Fred L. Templinc7dc89c2007-11-29 22:11:40 +110069/* Special-Use IPv4 Addresses (RFC3330) */
Joe Perches2658fa82007-12-16 13:42:49 -080070
71static inline bool ipv4_is_private_10(__be32 addr)
72{
73 return (addr & htonl(0xff000000)) == htonl(0x0a000000);
74}
75
76static inline bool ipv4_is_private_172(__be32 addr)
77{
78 return (addr & htonl(0xfff00000)) == htonl(0xac100000);
79}
80
81static inline bool ipv4_is_private_192(__be32 addr)
82{
83 return (addr & htonl(0xffff0000)) == htonl(0xc0a80000);
84}
85
86static inline bool ipv4_is_linklocal_169(__be32 addr)
87{
88 return (addr & htonl(0xffff0000)) == htonl(0xa9fe0000);
89}
90
91static inline bool ipv4_is_anycast_6to4(__be32 addr)
92{
93 return (addr & htonl(0xffffff00)) == htonl(0xc0586300);
94}
95
96static inline bool ipv4_is_test_192(__be32 addr)
97{
98 return (addr & htonl(0xffffff00)) == htonl(0xc0000200);
99}
100
101static inline bool ipv4_is_test_198(__be32 addr)
102{
103 return (addr & htonl(0xfffe0000)) == htonl(0xc6120000);
104}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif /* _LINUX_IN_H */