ip.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  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 for the IP module.
  8. *
  9. * Version: @(#)ip.h 1.0.2 05/07/93
  10. *
  11. * Authors: Ross Biro
  12. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  13. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  14. *
  15. * Changes:
  16. * Mike McLagan : Routing by source
  17. */
  18. #ifndef _IP_H
  19. #define _IP_H
  20. #include <linux/types.h>
  21. #include <linux/ip.h>
  22. #include <linux/in.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/jhash.h>
  25. #include <linux/sockptr.h>
  26. #include <linux/static_key.h>
  27. #include <net/inet_sock.h>
  28. #include <net/route.h>
  29. #include <net/snmp.h>
  30. #include <net/flow.h>
  31. #include <net/flow_dissector.h>
  32. #include <net/netns/hash.h>
  33. #include <net/lwtunnel.h>
  34. #include <net/inet_dscp.h>
  35. #define IPV4_MAX_PMTU 65535U /* RFC 2675, Section 5.1 */
  36. #define IPV4_MIN_MTU 68 /* RFC 791 */
  37. extern unsigned int sysctl_fib_sync_mem;
  38. extern unsigned int sysctl_fib_sync_mem_min;
  39. extern unsigned int sysctl_fib_sync_mem_max;
  40. struct sock;
  41. struct inet_skb_parm {
  42. int iif;
  43. struct ip_options opt; /* Compiled IP options */
  44. u16 flags;
  45. #define IPSKB_FORWARDED BIT(0)
  46. #define IPSKB_XFRM_TUNNEL_SIZE BIT(1)
  47. #define IPSKB_XFRM_TRANSFORMED BIT(2)
  48. #define IPSKB_FRAG_COMPLETE BIT(3)
  49. #define IPSKB_REROUTED BIT(4)
  50. #define IPSKB_DOREDIRECT BIT(5)
  51. #define IPSKB_FRAG_PMTU BIT(6)
  52. #define IPSKB_L3SLAVE BIT(7)
  53. #define IPSKB_NOPOLICY BIT(8)
  54. #define IPSKB_MULTIPATH BIT(9)
  55. #define IPSKB_MCROUTE BIT(10)
  56. u16 frag_max_size;
  57. };
  58. static inline bool ipv4_l3mdev_skb(u16 flags)
  59. {
  60. return !!(flags & IPSKB_L3SLAVE);
  61. }
  62. static inline unsigned int ip_hdrlen(const struct sk_buff *skb)
  63. {
  64. return ip_hdr(skb)->ihl * 4;
  65. }
  66. struct ipcm_cookie {
  67. struct sockcm_cookie sockc;
  68. __be32 addr;
  69. int oif;
  70. struct ip_options_rcu *opt;
  71. __u8 protocol;
  72. __u8 ttl;
  73. __s16 tos;
  74. __u16 gso_size;
  75. };
  76. static inline void ipcm_init(struct ipcm_cookie *ipcm)
  77. {
  78. *ipcm = (struct ipcm_cookie) { .tos = -1 };
  79. }
  80. static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
  81. const struct inet_sock *inet)
  82. {
  83. *ipcm = (struct ipcm_cookie) {
  84. .tos = READ_ONCE(inet->tos),
  85. };
  86. sockcm_init(&ipcm->sockc, &inet->sk);
  87. ipcm->oif = READ_ONCE(inet->sk.sk_bound_dev_if);
  88. ipcm->addr = inet->inet_saddr;
  89. ipcm->protocol = READ_ONCE(inet->inet_num);
  90. }
  91. #define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb))
  92. #define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb))
  93. /* return enslaved device index if relevant */
  94. static inline int inet_sdif(const struct sk_buff *skb)
  95. {
  96. #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  97. if (skb && ipv4_l3mdev_skb(IPCB(skb)->flags))
  98. return IPCB(skb)->iif;
  99. #endif
  100. return 0;
  101. }
  102. /* Special input handler for packets caught by router alert option.
  103. They are selected only by protocol field, and then processed likely
  104. local ones; but only if someone wants them! Otherwise, router
  105. not running rsvpd will kill RSVP.
  106. It is user level problem, what it will make with them.
  107. I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
  108. but receiver should be enough clever f.e. to forward mtrace requests,
  109. sent to multicast group to reach destination designated router.
  110. */
  111. struct ip_ra_chain {
  112. struct ip_ra_chain __rcu *next;
  113. struct sock *sk;
  114. union {
  115. void (*destructor)(struct sock *);
  116. struct sock *saved_sk;
  117. };
  118. struct rcu_head rcu;
  119. };
  120. /* IP flags. */
  121. #define IP_CE 0x8000 /* Flag: "Congestion" */
  122. #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
  123. #define IP_MF 0x2000 /* Flag: "More Fragments" */
  124. #define IP_OFFSET 0x1FFF /* "Fragment Offset" part */
  125. #define IP_FRAG_TIME (30 * HZ) /* fragment lifetime */
  126. struct msghdr;
  127. struct net_device;
  128. struct packet_type;
  129. struct rtable;
  130. struct sockaddr;
  131. int igmp_mc_init(void);
  132. /*
  133. * Functions provided by ip.c
  134. */
  135. int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
  136. __be32 saddr, __be32 daddr,
  137. struct ip_options_rcu *opt, u8 tos);
  138. int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
  139. struct net_device *orig_dev);
  140. void ip_list_rcv(struct list_head *head, struct packet_type *pt,
  141. struct net_device *orig_dev);
  142. int ip_local_deliver(struct sk_buff *skb);
  143. void ip_protocol_deliver_rcu(struct net *net, struct sk_buff *skb, int proto);
  144. int ip_mr_input(struct sk_buff *skb);
  145. int ip_mr_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  146. int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  147. int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  148. int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
  149. int (*output)(struct net *, struct sock *, struct sk_buff *));
  150. struct ip_fraglist_iter {
  151. struct sk_buff *frag;
  152. struct iphdr *iph;
  153. int offset;
  154. unsigned int hlen;
  155. };
  156. void ip_fraglist_init(struct sk_buff *skb, struct iphdr *iph,
  157. unsigned int hlen, struct ip_fraglist_iter *iter);
  158. void ip_fraglist_prepare(struct sk_buff *skb, struct ip_fraglist_iter *iter);
  159. static inline struct sk_buff *ip_fraglist_next(struct ip_fraglist_iter *iter)
  160. {
  161. struct sk_buff *skb = iter->frag;
  162. iter->frag = skb->next;
  163. skb_mark_not_on_list(skb);
  164. return skb;
  165. }
  166. struct ip_frag_state {
  167. bool DF;
  168. unsigned int hlen;
  169. unsigned int ll_rs;
  170. unsigned int mtu;
  171. unsigned int left;
  172. int offset;
  173. int ptr;
  174. __be16 not_last_frag;
  175. };
  176. void ip_frag_init(struct sk_buff *skb, unsigned int hlen, unsigned int ll_rs,
  177. unsigned int mtu, bool DF, struct ip_frag_state *state);
  178. struct sk_buff *ip_frag_next(struct sk_buff *skb,
  179. struct ip_frag_state *state);
  180. void ip_send_check(struct iphdr *ip);
  181. int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  182. int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  183. int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
  184. __u8 tos);
  185. void ip_init(void);
  186. int ip_append_data(struct sock *sk, struct flowi4 *fl4,
  187. int getfrag(void *from, char *to, int offset, int len,
  188. int odd, struct sk_buff *skb),
  189. void *from, int len, int protolen,
  190. struct ipcm_cookie *ipc,
  191. struct rtable **rt,
  192. unsigned int flags);
  193. int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd,
  194. struct sk_buff *skb);
  195. struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4,
  196. struct sk_buff_head *queue,
  197. struct inet_cork *cork);
  198. int ip_send_skb(struct net *net, struct sk_buff *skb);
  199. int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4);
  200. void ip_flush_pending_frames(struct sock *sk);
  201. struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4,
  202. int getfrag(void *from, char *to, int offset,
  203. int len, int odd, struct sk_buff *skb),
  204. void *from, int length, int transhdrlen,
  205. struct ipcm_cookie *ipc, struct rtable **rtp,
  206. struct inet_cork *cork, unsigned int flags);
  207. int ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl);
  208. static inline struct sk_buff *ip_finish_skb(struct sock *sk, struct flowi4 *fl4)
  209. {
  210. return __ip_make_skb(sk, fl4, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
  211. }
  212. /* Get the route scope that should be used when sending a packet. */
  213. static inline u8 ip_sendmsg_scope(const struct inet_sock *inet,
  214. const struct ipcm_cookie *ipc,
  215. const struct msghdr *msg)
  216. {
  217. if (sock_flag(&inet->sk, SOCK_LOCALROUTE) ||
  218. msg->msg_flags & MSG_DONTROUTE ||
  219. (ipc->opt && ipc->opt->opt.is_strictroute))
  220. return RT_SCOPE_LINK;
  221. return RT_SCOPE_UNIVERSE;
  222. }
  223. /* datagram.c */
  224. int __ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len);
  225. int ip4_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len);
  226. void ip4_datagram_release_cb(struct sock *sk);
  227. struct ip_reply_arg {
  228. struct kvec iov[1];
  229. int flags;
  230. __wsum csum;
  231. int csumoffset; /* u16 offset of csum in iov[0].iov_base */
  232. /* -1 if not needed */
  233. int bound_dev_if;
  234. u8 tos;
  235. kuid_t uid;
  236. };
  237. #define IP_REPLY_ARG_NOSRCCHECK 1
  238. static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg)
  239. {
  240. return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0;
  241. }
  242. void ip_send_unicast_reply(struct sock *sk, const struct sock *orig_sk,
  243. struct sk_buff *skb,
  244. const struct ip_options *sopt,
  245. __be32 daddr, __be32 saddr,
  246. const struct ip_reply_arg *arg,
  247. unsigned int len, u64 transmit_time, u32 txhash);
  248. #define IP_INC_STATS(net, field) SNMP_INC_STATS64((net)->mib.ip_statistics, field)
  249. #define __IP_INC_STATS(net, field) __SNMP_INC_STATS64((net)->mib.ip_statistics, field)
  250. #define IP_ADD_STATS(net, field, val) SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
  251. #define __IP_ADD_STATS(net, field, val) __SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
  252. #define IP_UPD_PO_STATS(net, field, val) SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
  253. #define __IP_UPD_PO_STATS(net, field, val) __SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
  254. #define NET_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.net_statistics, field)
  255. #define __NET_INC_STATS(net, field) __SNMP_INC_STATS((net)->mib.net_statistics, field)
  256. #define NET_ADD_STATS(net, field, adnd) SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
  257. #define __NET_ADD_STATS(net, field, adnd) __SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
  258. static inline u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offt)
  259. {
  260. return *(((unsigned long *)per_cpu_ptr(mib, cpu)) + offt);
  261. }
  262. unsigned long snmp_fold_field(void __percpu *mib, int offt);
  263. #if BITS_PER_LONG==32
  264. u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
  265. size_t syncp_offset);
  266. u64 snmp_fold_field64(void __percpu *mib, int offt, size_t sync_off);
  267. #else
  268. static inline u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
  269. size_t syncp_offset)
  270. {
  271. return snmp_get_cpu_field(mib, cpu, offct);
  272. }
  273. static inline u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_off)
  274. {
  275. return snmp_fold_field(mib, offt);
  276. }
  277. #endif
  278. #define snmp_get_cpu_field64_batch_cnt(buff64, stats_list, cnt, \
  279. mib_statistic, offset) \
  280. { \
  281. int i, c; \
  282. for_each_possible_cpu(c) { \
  283. for (i = 0; i < cnt; i++) \
  284. buff64[i] += snmp_get_cpu_field64( \
  285. mib_statistic, \
  286. c, stats_list[i].entry, \
  287. offset); \
  288. } \
  289. }
  290. #define snmp_get_cpu_field_batch_cnt(buff, stats_list, cnt, mib_statistic) \
  291. { \
  292. int i, c; \
  293. for_each_possible_cpu(c) { \
  294. for (i = 0; i < cnt; i++) \
  295. buff[i] += snmp_get_cpu_field( \
  296. mib_statistic, \
  297. c, stats_list[i].entry); \
  298. } \
  299. }
  300. static inline void inet_get_local_port_range(const struct net *net, int *low, int *high)
  301. {
  302. u32 range = READ_ONCE(net->ipv4.ip_local_ports.range);
  303. *low = range & 0xffff;
  304. *high = range >> 16;
  305. }
  306. bool inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high);
  307. #ifdef CONFIG_SYSCTL
  308. static inline bool inet_is_local_reserved_port(const struct net *net, unsigned short port)
  309. {
  310. if (!net->ipv4.sysctl_local_reserved_ports)
  311. return false;
  312. return test_bit(port, net->ipv4.sysctl_local_reserved_ports);
  313. }
  314. static inline bool sysctl_dev_name_is_allowed(const char *name)
  315. {
  316. return strcmp(name, "default") != 0 && strcmp(name, "all") != 0;
  317. }
  318. static inline bool inet_port_requires_bind_service(struct net *net, unsigned short port)
  319. {
  320. return port < READ_ONCE(net->ipv4.sysctl_ip_prot_sock);
  321. }
  322. #else
  323. static inline bool inet_is_local_reserved_port(struct net *net, unsigned short port)
  324. {
  325. return false;
  326. }
  327. static inline bool inet_port_requires_bind_service(struct net *net, unsigned short port)
  328. {
  329. return port < PROT_SOCK;
  330. }
  331. #endif
  332. __be32 inet_current_timestamp(void);
  333. /* From inetpeer.c */
  334. extern int inet_peer_threshold;
  335. extern int inet_peer_minttl;
  336. extern int inet_peer_maxttl;
  337. void ipfrag_init(void);
  338. void ip_static_sysctl_init(void);
  339. #define IP4_REPLY_MARK(net, mark) \
  340. (READ_ONCE((net)->ipv4.sysctl_fwmark_reflect) ? (mark) : 0)
  341. static inline bool ip_is_fragment(const struct iphdr *iph)
  342. {
  343. return (iph->frag_off & htons(IP_MF | IP_OFFSET)) != 0;
  344. }
  345. #ifdef CONFIG_INET
  346. #include <net/dst.h>
  347. /* The function in 2.2 was invalid, producing wrong result for
  348. * check=0xFEFF. It was noticed by Arthur Skawina _year_ ago. --ANK(000625) */
  349. static inline
  350. int ip_decrease_ttl(struct iphdr *iph)
  351. {
  352. u32 check = (__force u32)iph->check;
  353. check += (__force u32)htons(0x0100);
  354. iph->check = (__force __sum16)(check + (check>=0xFFFF));
  355. return --iph->ttl;
  356. }
  357. static inline dscp_t ip4h_dscp(const struct iphdr *ip4h)
  358. {
  359. return inet_dsfield_to_dscp(ip4h->tos);
  360. }
  361. static inline int ip_mtu_locked(const struct dst_entry *dst)
  362. {
  363. const struct rtable *rt = dst_rtable(dst);
  364. return rt->rt_mtu_locked || dst_metric_locked(dst, RTAX_MTU);
  365. }
  366. static inline
  367. int ip_dont_fragment(const struct sock *sk, const struct dst_entry *dst)
  368. {
  369. u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc);
  370. return pmtudisc == IP_PMTUDISC_DO ||
  371. (pmtudisc == IP_PMTUDISC_WANT &&
  372. !ip_mtu_locked(dst));
  373. }
  374. static inline bool ip_sk_accept_pmtu(const struct sock *sk)
  375. {
  376. u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc);
  377. return pmtudisc != IP_PMTUDISC_INTERFACE &&
  378. pmtudisc != IP_PMTUDISC_OMIT;
  379. }
  380. static inline bool ip_sk_use_pmtu(const struct sock *sk)
  381. {
  382. return READ_ONCE(inet_sk(sk)->pmtudisc) < IP_PMTUDISC_PROBE;
  383. }
  384. static inline bool ip_sk_ignore_df(const struct sock *sk)
  385. {
  386. u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc);
  387. return pmtudisc < IP_PMTUDISC_DO || pmtudisc == IP_PMTUDISC_OMIT;
  388. }
  389. static inline unsigned int ip_dst_mtu_maybe_forward(const struct dst_entry *dst,
  390. bool forwarding)
  391. {
  392. const struct rtable *rt = dst_rtable(dst);
  393. const struct net_device *dev;
  394. unsigned int mtu, res;
  395. struct net *net;
  396. rcu_read_lock();
  397. dev = dst_dev_rcu(dst);
  398. net = dev_net_rcu(dev);
  399. if (READ_ONCE(net->ipv4.sysctl_ip_fwd_use_pmtu) ||
  400. ip_mtu_locked(dst) ||
  401. !forwarding) {
  402. mtu = rt->rt_pmtu;
  403. if (mtu && time_before(jiffies, READ_ONCE(rt->dst.expires)))
  404. goto out;
  405. }
  406. /* 'forwarding = true' case should always honour route mtu */
  407. mtu = dst_metric_raw(dst, RTAX_MTU);
  408. if (mtu)
  409. goto out;
  410. mtu = READ_ONCE(dev->mtu);
  411. if (unlikely(ip_mtu_locked(dst))) {
  412. if (rt->rt_uses_gateway && mtu > 576)
  413. mtu = 576;
  414. }
  415. out:
  416. mtu = min_t(unsigned int, mtu, IP_MAX_MTU);
  417. res = mtu - lwtunnel_headroom(dst->lwtstate, mtu);
  418. rcu_read_unlock();
  419. return res;
  420. }
  421. static inline unsigned int ip_skb_dst_mtu(struct sock *sk,
  422. const struct sk_buff *skb)
  423. {
  424. const struct dst_entry *dst = skb_dst(skb);
  425. unsigned int mtu;
  426. if (!sk || !sk_fullsock(sk) || ip_sk_use_pmtu(sk)) {
  427. bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED;
  428. return ip_dst_mtu_maybe_forward(dst, forwarding);
  429. }
  430. mtu = min(READ_ONCE(dst_dev(dst)->mtu), IP_MAX_MTU);
  431. return mtu - lwtunnel_headroom(dst->lwtstate, mtu);
  432. }
  433. struct dst_metrics *ip_fib_metrics_init(struct nlattr *fc_mx, int fc_mx_len,
  434. struct netlink_ext_ack *extack);
  435. static inline void ip_fib_metrics_put(struct dst_metrics *fib_metrics)
  436. {
  437. if (fib_metrics != &dst_default_metrics &&
  438. refcount_dec_and_test(&fib_metrics->refcnt))
  439. kfree(fib_metrics);
  440. }
  441. /* ipv4 and ipv6 both use refcounted metrics if it is not the default */
  442. static inline
  443. void ip_dst_init_metrics(struct dst_entry *dst, struct dst_metrics *fib_metrics)
  444. {
  445. dst_init_metrics(dst, fib_metrics->metrics, true);
  446. if (fib_metrics != &dst_default_metrics) {
  447. dst->_metrics |= DST_METRICS_REFCOUNTED;
  448. refcount_inc(&fib_metrics->refcnt);
  449. }
  450. }
  451. static inline
  452. void ip_dst_metrics_put(struct dst_entry *dst)
  453. {
  454. struct dst_metrics *p = (struct dst_metrics *)DST_METRICS_PTR(dst);
  455. if (p != &dst_default_metrics && refcount_dec_and_test(&p->refcnt))
  456. kfree(p);
  457. }
  458. void __ip_select_ident(struct net *net, struct iphdr *iph, int segs);
  459. static inline void ip_select_ident_segs(struct net *net, struct sk_buff *skb,
  460. struct sock *sk, int segs)
  461. {
  462. struct iphdr *iph = ip_hdr(skb);
  463. /* We had many attacks based on IPID, use the private
  464. * generator as much as we can.
  465. */
  466. if (sk && inet_sk(sk)->inet_daddr) {
  467. int val;
  468. /* avoid atomic operations for TCP,
  469. * as we hold socket lock at this point.
  470. */
  471. if (sk_is_tcp(sk)) {
  472. sock_owned_by_me(sk);
  473. val = atomic_read(&inet_sk(sk)->inet_id);
  474. atomic_set(&inet_sk(sk)->inet_id, val + segs);
  475. } else {
  476. val = atomic_add_return(segs, &inet_sk(sk)->inet_id);
  477. }
  478. iph->id = htons(val);
  479. return;
  480. }
  481. if ((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) {
  482. iph->id = 0;
  483. } else {
  484. /* Unfortunately we need the big hammer to get a suitable IPID */
  485. __ip_select_ident(net, iph, segs);
  486. }
  487. }
  488. static inline void ip_select_ident(struct net *net, struct sk_buff *skb,
  489. struct sock *sk)
  490. {
  491. ip_select_ident_segs(net, skb, sk, 1);
  492. }
  493. static inline __wsum inet_compute_pseudo(struct sk_buff *skb, int proto)
  494. {
  495. return csum_tcpudp_nofold(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  496. skb->len, proto, 0);
  497. }
  498. /* copy IPv4 saddr & daddr to flow_keys, possibly using 64bit load/store
  499. * Equivalent to : flow->v4addrs.src = iph->saddr;
  500. * flow->v4addrs.dst = iph->daddr;
  501. */
  502. static inline void iph_to_flow_copy_v4addrs(struct flow_keys *flow,
  503. const struct iphdr *iph)
  504. {
  505. BUILD_BUG_ON(offsetof(typeof(flow->addrs), v4addrs.dst) !=
  506. offsetof(typeof(flow->addrs), v4addrs.src) +
  507. sizeof(flow->addrs.v4addrs.src));
  508. memcpy(&flow->addrs.v4addrs, &iph->addrs, sizeof(flow->addrs.v4addrs));
  509. flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  510. }
  511. /*
  512. * Map a multicast IP onto multicast MAC for type ethernet.
  513. */
  514. static inline void ip_eth_mc_map(__be32 naddr, char *buf)
  515. {
  516. __u32 addr=ntohl(naddr);
  517. buf[0]=0x01;
  518. buf[1]=0x00;
  519. buf[2]=0x5e;
  520. buf[5]=addr&0xFF;
  521. addr>>=8;
  522. buf[4]=addr&0xFF;
  523. addr>>=8;
  524. buf[3]=addr&0x7F;
  525. }
  526. /*
  527. * Map a multicast IP onto multicast MAC for type IP-over-InfiniBand.
  528. * Leave P_Key as 0 to be filled in by driver.
  529. */
  530. static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
  531. {
  532. __u32 addr;
  533. unsigned char scope = broadcast[5] & 0xF;
  534. buf[0] = 0; /* Reserved */
  535. buf[1] = 0xff; /* Multicast QPN */
  536. buf[2] = 0xff;
  537. buf[3] = 0xff;
  538. addr = ntohl(naddr);
  539. buf[4] = 0xff;
  540. buf[5] = 0x10 | scope; /* scope from broadcast address */
  541. buf[6] = 0x40; /* IPv4 signature */
  542. buf[7] = 0x1b;
  543. buf[8] = broadcast[8]; /* P_Key */
  544. buf[9] = broadcast[9];
  545. buf[10] = 0;
  546. buf[11] = 0;
  547. buf[12] = 0;
  548. buf[13] = 0;
  549. buf[14] = 0;
  550. buf[15] = 0;
  551. buf[19] = addr & 0xff;
  552. addr >>= 8;
  553. buf[18] = addr & 0xff;
  554. addr >>= 8;
  555. buf[17] = addr & 0xff;
  556. addr >>= 8;
  557. buf[16] = addr & 0x0f;
  558. }
  559. static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
  560. {
  561. if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0)
  562. memcpy(buf, broadcast, 4);
  563. else
  564. memcpy(buf, &naddr, sizeof(naddr));
  565. }
  566. #if IS_ENABLED(CONFIG_IPV6)
  567. #include <linux/ipv6.h>
  568. #endif
  569. static __inline__ void inet_reset_saddr(struct sock *sk)
  570. {
  571. inet_sk(sk)->inet_rcv_saddr = inet_sk(sk)->inet_saddr = 0;
  572. #if IS_ENABLED(CONFIG_IPV6)
  573. if (sk->sk_family == PF_INET6) {
  574. struct ipv6_pinfo *np = inet6_sk(sk);
  575. memset(&np->saddr, 0, sizeof(np->saddr));
  576. memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
  577. }
  578. #endif
  579. }
  580. #endif
  581. #if IS_MODULE(CONFIG_IPV6)
  582. #define EXPORT_IPV6_MOD(X) EXPORT_SYMBOL(X)
  583. #define EXPORT_IPV6_MOD_GPL(X) EXPORT_SYMBOL_GPL(X)
  584. #else
  585. #define EXPORT_IPV6_MOD(X)
  586. #define EXPORT_IPV6_MOD_GPL(X)
  587. #endif
  588. static inline unsigned int ipv4_addr_hash(__be32 ip)
  589. {
  590. return (__force unsigned int) ip;
  591. }
  592. static inline u32 __ipv4_addr_hash(const __be32 ip, const u32 initval)
  593. {
  594. return jhash_1word((__force u32)ip, initval);
  595. }
  596. static inline u32 ipv4_portaddr_hash(const struct net *net,
  597. __be32 saddr,
  598. unsigned int port)
  599. {
  600. return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
  601. }
  602. bool ip_call_ra_chain(struct sk_buff *skb);
  603. /*
  604. * Functions provided by ip_fragment.c
  605. */
  606. enum ip_defrag_users {
  607. IP_DEFRAG_LOCAL_DELIVER,
  608. IP_DEFRAG_CALL_RA_CHAIN,
  609. IP_DEFRAG_CONNTRACK_IN,
  610. __IP_DEFRAG_CONNTRACK_IN_END = IP_DEFRAG_CONNTRACK_IN + USHRT_MAX,
  611. IP_DEFRAG_CONNTRACK_OUT,
  612. __IP_DEFRAG_CONNTRACK_OUT_END = IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX,
  613. IP_DEFRAG_CONNTRACK_BRIDGE_IN,
  614. __IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX,
  615. IP_DEFRAG_VS_IN,
  616. IP_DEFRAG_VS_OUT,
  617. IP_DEFRAG_VS_FWD,
  618. IP_DEFRAG_AF_PACKET,
  619. IP_DEFRAG_MACVLAN,
  620. };
  621. /* Return true if the value of 'user' is between 'lower_bond'
  622. * and 'upper_bond' inclusively.
  623. */
  624. static inline bool ip_defrag_user_in_between(u32 user,
  625. enum ip_defrag_users lower_bond,
  626. enum ip_defrag_users upper_bond)
  627. {
  628. return user >= lower_bond && user <= upper_bond;
  629. }
  630. int ip_defrag(struct net *net, struct sk_buff *skb, u32 user);
  631. #ifdef CONFIG_INET
  632. struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user);
  633. #else
  634. static inline struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
  635. {
  636. return skb;
  637. }
  638. #endif
  639. /*
  640. * Functions provided by ip_forward.c
  641. */
  642. int ip_forward(struct sk_buff *skb);
  643. /*
  644. * Functions provided by ip_options.c
  645. */
  646. void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
  647. __be32 daddr, struct rtable *rt);
  648. int __ip_options_echo(struct net *net, struct ip_options *dopt,
  649. struct sk_buff *skb, const struct ip_options *sopt);
  650. static inline int ip_options_echo(struct net *net, struct ip_options *dopt,
  651. struct sk_buff *skb)
  652. {
  653. return __ip_options_echo(net, dopt, skb, &IPCB(skb)->opt);
  654. }
  655. void ip_options_fragment(struct sk_buff *skb);
  656. int __ip_options_compile(struct net *net, struct ip_options *opt,
  657. struct sk_buff *skb, __be32 *info);
  658. int ip_options_compile(struct net *net, struct ip_options *opt,
  659. struct sk_buff *skb);
  660. int ip_options_get(struct net *net, struct ip_options_rcu **optp,
  661. sockptr_t data, int optlen);
  662. void ip_options_undo(struct ip_options *opt);
  663. void ip_forward_options(struct sk_buff *skb);
  664. int ip_options_rcv_srr(struct sk_buff *skb, struct net_device *dev);
  665. /*
  666. * Functions provided by ip_sockglue.c
  667. */
  668. void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb, bool drop_dst);
  669. void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
  670. struct sk_buff *skb, int tlen, int offset);
  671. int ip_cmsg_send(struct sock *sk, struct msghdr *msg,
  672. struct ipcm_cookie *ipc, bool allow_ipv6);
  673. DECLARE_STATIC_KEY_FALSE(ip4_min_ttl);
  674. int do_ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
  675. unsigned int optlen);
  676. int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
  677. unsigned int optlen);
  678. int do_ip_getsockopt(struct sock *sk, int level, int optname,
  679. sockptr_t optval, sockptr_t optlen);
  680. int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
  681. int __user *optlen);
  682. int ip_ra_control(struct sock *sk, unsigned char on,
  683. void (*destructor)(struct sock *));
  684. int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len);
  685. void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
  686. u32 info, u8 *payload);
  687. void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport,
  688. u32 info);
  689. static inline void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
  690. {
  691. ip_cmsg_recv_offset(msg, skb->sk, skb, 0, 0);
  692. }
  693. bool icmp_global_allow(struct net *net);
  694. void icmp_global_consume(struct net *net);
  695. #ifdef CONFIG_PROC_FS
  696. int ip_misc_proc_init(void);
  697. #endif
  698. int rtm_getroute_parse_ip_proto(struct nlattr *attr, u8 *ip_proto, u8 family,
  699. struct netlink_ext_ack *extack);
  700. static inline bool inetdev_valid_mtu(unsigned int mtu)
  701. {
  702. return likely(mtu >= IPV4_MIN_MTU);
  703. }
  704. void ip_sock_set_freebind(struct sock *sk);
  705. int ip_sock_set_mtu_discover(struct sock *sk, int val);
  706. void ip_sock_set_pktinfo(struct sock *sk);
  707. void ip_sock_set_recverr(struct sock *sk);
  708. void ip_sock_set_tos(struct sock *sk, int val);
  709. void __ip_sock_set_tos(struct sock *sk, int val);
  710. #endif /* _IP_H */