ip.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * Definitions for the IP module.
  7. *
  8. * Version: @(#)ip.h 1.0.2 05/07/93
  9. *
  10. * Authors: Ross Biro
  11. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  12. * Alan Cox, <gw4pts@gw4pts.ampr.org>
  13. *
  14. * Changes:
  15. * Mike McLagan : Routing by source
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. */
  22. #ifndef _IP_H
  23. #define _IP_H
  24. #include <linux/types.h>
  25. #include <linux/ip.h>
  26. #include <linux/in.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/jhash.h>
  29. #include <net/inet_sock.h>
  30. #include <net/route.h>
  31. #include <net/snmp.h>
  32. #include <net/flow.h>
  33. #include <net/flow_dissector.h>
  34. #include <net/netns/hash.h>
  35. #define IPV4_MAX_PMTU 65535U /* RFC 2675, Section 5.1 */
  36. #define IPV4_MIN_MTU 68 /* RFC 791 */
  37. struct sock;
  38. struct inet_skb_parm {
  39. int iif;
  40. struct ip_options opt; /* Compiled IP options */
  41. u16 flags;
  42. #define IPSKB_FORWARDED BIT(0)
  43. #define IPSKB_XFRM_TUNNEL_SIZE BIT(1)
  44. #define IPSKB_XFRM_TRANSFORMED BIT(2)
  45. #define IPSKB_FRAG_COMPLETE BIT(3)
  46. #define IPSKB_REROUTED BIT(4)
  47. #define IPSKB_DOREDIRECT BIT(5)
  48. #define IPSKB_FRAG_PMTU BIT(6)
  49. #define IPSKB_L3SLAVE BIT(7)
  50. u16 frag_max_size;
  51. };
  52. static inline bool ipv4_l3mdev_skb(u16 flags)
  53. {
  54. return !!(flags & IPSKB_L3SLAVE);
  55. }
  56. static inline unsigned int ip_hdrlen(const struct sk_buff *skb)
  57. {
  58. return ip_hdr(skb)->ihl * 4;
  59. }
  60. struct ipcm_cookie {
  61. struct sockcm_cookie sockc;
  62. __be32 addr;
  63. int oif;
  64. struct ip_options_rcu *opt;
  65. __u8 ttl;
  66. __s16 tos;
  67. char priority;
  68. __u16 gso_size;
  69. };
  70. static inline void ipcm_init(struct ipcm_cookie *ipcm)
  71. {
  72. *ipcm = (struct ipcm_cookie) { .tos = -1 };
  73. }
  74. static inline void ipcm_init_sk(struct ipcm_cookie *ipcm,
  75. const struct inet_sock *inet)
  76. {
  77. ipcm_init(ipcm);
  78. ipcm->sockc.tsflags = inet->sk.sk_tsflags;
  79. ipcm->oif = inet->sk.sk_bound_dev_if;
  80. ipcm->addr = inet->inet_saddr;
  81. }
  82. #define IPCB(skb) ((struct inet_skb_parm*)((skb)->cb))
  83. #define PKTINFO_SKB_CB(skb) ((struct in_pktinfo *)((skb)->cb))
  84. /* return enslaved device index if relevant */
  85. static inline int inet_sdif(struct sk_buff *skb)
  86. {
  87. #if IS_ENABLED(CONFIG_NET_L3_MASTER_DEV)
  88. if (skb && ipv4_l3mdev_skb(IPCB(skb)->flags))
  89. return IPCB(skb)->iif;
  90. #endif
  91. return 0;
  92. }
  93. /* Special input handler for packets caught by router alert option.
  94. They are selected only by protocol field, and then processed likely
  95. local ones; but only if someone wants them! Otherwise, router
  96. not running rsvpd will kill RSVP.
  97. It is user level problem, what it will make with them.
  98. I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
  99. but receiver should be enough clever f.e. to forward mtrace requests,
  100. sent to multicast group to reach destination designated router.
  101. */
  102. struct ip_ra_chain {
  103. struct ip_ra_chain __rcu *next;
  104. struct sock *sk;
  105. union {
  106. void (*destructor)(struct sock *);
  107. struct sock *saved_sk;
  108. };
  109. struct rcu_head rcu;
  110. };
  111. /* IP flags. */
  112. #define IP_CE 0x8000 /* Flag: "Congestion" */
  113. #define IP_DF 0x4000 /* Flag: "Don't Fragment" */
  114. #define IP_MF 0x2000 /* Flag: "More Fragments" */
  115. #define IP_OFFSET 0x1FFF /* "Fragment Offset" part */
  116. #define IP_FRAG_TIME (30 * HZ) /* fragment lifetime */
  117. struct msghdr;
  118. struct net_device;
  119. struct packet_type;
  120. struct rtable;
  121. struct sockaddr;
  122. int igmp_mc_init(void);
  123. /*
  124. * Functions provided by ip.c
  125. */
  126. int ip_build_and_send_pkt(struct sk_buff *skb, const struct sock *sk,
  127. __be32 saddr, __be32 daddr,
  128. struct ip_options_rcu *opt);
  129. int ip_rcv(struct sk_buff *skb, struct net_device *dev, struct packet_type *pt,
  130. struct net_device *orig_dev);
  131. void ip_list_rcv(struct list_head *head, struct packet_type *pt,
  132. struct net_device *orig_dev);
  133. int ip_local_deliver(struct sk_buff *skb);
  134. int ip_mr_input(struct sk_buff *skb);
  135. int ip_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  136. int ip_mc_output(struct net *net, struct sock *sk, struct sk_buff *skb);
  137. int ip_do_fragment(struct net *net, struct sock *sk, struct sk_buff *skb,
  138. int (*output)(struct net *, struct sock *, struct sk_buff *));
  139. void ip_send_check(struct iphdr *ip);
  140. int __ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  141. int ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb);
  142. int __ip_queue_xmit(struct sock *sk, struct sk_buff *skb, struct flowi *fl,
  143. __u8 tos);
  144. void ip_init(void);
  145. int ip_append_data(struct sock *sk, struct flowi4 *fl4,
  146. int getfrag(void *from, char *to, int offset, int len,
  147. int odd, struct sk_buff *skb),
  148. void *from, int len, int protolen,
  149. struct ipcm_cookie *ipc,
  150. struct rtable **rt,
  151. unsigned int flags);
  152. int ip_generic_getfrag(void *from, char *to, int offset, int len, int odd,
  153. struct sk_buff *skb);
  154. ssize_t ip_append_page(struct sock *sk, struct flowi4 *fl4, struct page *page,
  155. int offset, size_t size, int flags);
  156. struct sk_buff *__ip_make_skb(struct sock *sk, struct flowi4 *fl4,
  157. struct sk_buff_head *queue,
  158. struct inet_cork *cork);
  159. int ip_send_skb(struct net *net, struct sk_buff *skb);
  160. int ip_push_pending_frames(struct sock *sk, struct flowi4 *fl4);
  161. void ip_flush_pending_frames(struct sock *sk);
  162. struct sk_buff *ip_make_skb(struct sock *sk, struct flowi4 *fl4,
  163. int getfrag(void *from, char *to, int offset,
  164. int len, int odd, struct sk_buff *skb),
  165. void *from, int length, int transhdrlen,
  166. struct ipcm_cookie *ipc, struct rtable **rtp,
  167. struct inet_cork *cork, unsigned int flags);
  168. static inline int ip_queue_xmit(struct sock *sk, struct sk_buff *skb,
  169. struct flowi *fl)
  170. {
  171. return __ip_queue_xmit(sk, skb, fl, inet_sk(sk)->tos);
  172. }
  173. static inline struct sk_buff *ip_finish_skb(struct sock *sk, struct flowi4 *fl4)
  174. {
  175. return __ip_make_skb(sk, fl4, &sk->sk_write_queue, &inet_sk(sk)->cork.base);
  176. }
  177. static inline __u8 get_rttos(struct ipcm_cookie* ipc, struct inet_sock *inet)
  178. {
  179. return (ipc->tos != -1) ? RT_TOS(ipc->tos) : RT_TOS(inet->tos);
  180. }
  181. static inline __u8 get_rtconn_flags(struct ipcm_cookie* ipc, struct sock* sk)
  182. {
  183. return (ipc->tos != -1) ? RT_CONN_FLAGS_TOS(sk, ipc->tos) : RT_CONN_FLAGS(sk);
  184. }
  185. /* datagram.c */
  186. int __ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
  187. int ip4_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len);
  188. void ip4_datagram_release_cb(struct sock *sk);
  189. struct ip_reply_arg {
  190. struct kvec iov[1];
  191. int flags;
  192. __wsum csum;
  193. int csumoffset; /* u16 offset of csum in iov[0].iov_base */
  194. /* -1 if not needed */
  195. int bound_dev_if;
  196. u8 tos;
  197. kuid_t uid;
  198. };
  199. #define IP_REPLY_ARG_NOSRCCHECK 1
  200. static inline __u8 ip_reply_arg_flowi_flags(const struct ip_reply_arg *arg)
  201. {
  202. return (arg->flags & IP_REPLY_ARG_NOSRCCHECK) ? FLOWI_FLAG_ANYSRC : 0;
  203. }
  204. void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
  205. const struct ip_options *sopt,
  206. __be32 daddr, __be32 saddr,
  207. const struct ip_reply_arg *arg,
  208. unsigned int len);
  209. #define IP_INC_STATS(net, field) SNMP_INC_STATS64((net)->mib.ip_statistics, field)
  210. #define __IP_INC_STATS(net, field) __SNMP_INC_STATS64((net)->mib.ip_statistics, field)
  211. #define IP_ADD_STATS(net, field, val) SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
  212. #define __IP_ADD_STATS(net, field, val) __SNMP_ADD_STATS64((net)->mib.ip_statistics, field, val)
  213. #define IP_UPD_PO_STATS(net, field, val) SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
  214. #define __IP_UPD_PO_STATS(net, field, val) __SNMP_UPD_PO_STATS64((net)->mib.ip_statistics, field, val)
  215. #define NET_INC_STATS(net, field) SNMP_INC_STATS((net)->mib.net_statistics, field)
  216. #define __NET_INC_STATS(net, field) __SNMP_INC_STATS((net)->mib.net_statistics, field)
  217. #define NET_ADD_STATS(net, field, adnd) SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
  218. #define __NET_ADD_STATS(net, field, adnd) __SNMP_ADD_STATS((net)->mib.net_statistics, field, adnd)
  219. u64 snmp_get_cpu_field(void __percpu *mib, int cpu, int offct);
  220. unsigned long snmp_fold_field(void __percpu *mib, int offt);
  221. #if BITS_PER_LONG==32
  222. u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
  223. size_t syncp_offset);
  224. u64 snmp_fold_field64(void __percpu *mib, int offt, size_t sync_off);
  225. #else
  226. static inline u64 snmp_get_cpu_field64(void __percpu *mib, int cpu, int offct,
  227. size_t syncp_offset)
  228. {
  229. return snmp_get_cpu_field(mib, cpu, offct);
  230. }
  231. static inline u64 snmp_fold_field64(void __percpu *mib, int offt, size_t syncp_off)
  232. {
  233. return snmp_fold_field(mib, offt);
  234. }
  235. #endif
  236. #define snmp_get_cpu_field64_batch(buff64, stats_list, mib_statistic, offset) \
  237. { \
  238. int i, c; \
  239. for_each_possible_cpu(c) { \
  240. for (i = 0; stats_list[i].name; i++) \
  241. buff64[i] += snmp_get_cpu_field64( \
  242. mib_statistic, \
  243. c, stats_list[i].entry, \
  244. offset); \
  245. } \
  246. }
  247. #define snmp_get_cpu_field_batch(buff, stats_list, mib_statistic) \
  248. { \
  249. int i, c; \
  250. for_each_possible_cpu(c) { \
  251. for (i = 0; stats_list[i].name; i++) \
  252. buff[i] += snmp_get_cpu_field( \
  253. mib_statistic, \
  254. c, stats_list[i].entry); \
  255. } \
  256. }
  257. void inet_get_local_port_range(struct net *net, int *low, int *high);
  258. #ifdef CONFIG_SYSCTL
  259. static inline int inet_is_local_reserved_port(struct net *net, int port)
  260. {
  261. if (!net->ipv4.sysctl_local_reserved_ports)
  262. return 0;
  263. return test_bit(port, net->ipv4.sysctl_local_reserved_ports);
  264. }
  265. static inline bool sysctl_dev_name_is_allowed(const char *name)
  266. {
  267. return strcmp(name, "default") != 0 && strcmp(name, "all") != 0;
  268. }
  269. static inline int inet_prot_sock(struct net *net)
  270. {
  271. return net->ipv4.sysctl_ip_prot_sock;
  272. }
  273. #else
  274. static inline int inet_is_local_reserved_port(struct net *net, int port)
  275. {
  276. return 0;
  277. }
  278. static inline int inet_prot_sock(struct net *net)
  279. {
  280. return PROT_SOCK;
  281. }
  282. #endif
  283. __be32 inet_current_timestamp(void);
  284. /* From inetpeer.c */
  285. extern int inet_peer_threshold;
  286. extern int inet_peer_minttl;
  287. extern int inet_peer_maxttl;
  288. void ipfrag_init(void);
  289. void ip_static_sysctl_init(void);
  290. #define IP4_REPLY_MARK(net, mark) \
  291. ((net)->ipv4.sysctl_fwmark_reflect ? (mark) : 0)
  292. static inline bool ip_is_fragment(const struct iphdr *iph)
  293. {
  294. return (iph->frag_off & htons(IP_MF | IP_OFFSET)) != 0;
  295. }
  296. #ifdef CONFIG_INET
  297. #include <net/dst.h>
  298. /* The function in 2.2 was invalid, producing wrong result for
  299. * check=0xFEFF. It was noticed by Arthur Skawina _year_ ago. --ANK(000625) */
  300. static inline
  301. int ip_decrease_ttl(struct iphdr *iph)
  302. {
  303. u32 check = (__force u32)iph->check;
  304. check += (__force u32)htons(0x0100);
  305. iph->check = (__force __sum16)(check + (check>=0xFFFF));
  306. return --iph->ttl;
  307. }
  308. static inline int ip_mtu_locked(const struct dst_entry *dst)
  309. {
  310. const struct rtable *rt = (const struct rtable *)dst;
  311. return rt->rt_mtu_locked || dst_metric_locked(dst, RTAX_MTU);
  312. }
  313. static inline
  314. int ip_dont_fragment(const struct sock *sk, const struct dst_entry *dst)
  315. {
  316. u8 pmtudisc = READ_ONCE(inet_sk(sk)->pmtudisc);
  317. return pmtudisc == IP_PMTUDISC_DO ||
  318. (pmtudisc == IP_PMTUDISC_WANT &&
  319. !ip_mtu_locked(dst));
  320. }
  321. static inline bool ip_sk_accept_pmtu(const struct sock *sk)
  322. {
  323. return inet_sk(sk)->pmtudisc != IP_PMTUDISC_INTERFACE &&
  324. inet_sk(sk)->pmtudisc != IP_PMTUDISC_OMIT;
  325. }
  326. static inline bool ip_sk_use_pmtu(const struct sock *sk)
  327. {
  328. return inet_sk(sk)->pmtudisc < IP_PMTUDISC_PROBE;
  329. }
  330. static inline bool ip_sk_ignore_df(const struct sock *sk)
  331. {
  332. return inet_sk(sk)->pmtudisc < IP_PMTUDISC_DO ||
  333. inet_sk(sk)->pmtudisc == IP_PMTUDISC_OMIT;
  334. }
  335. static inline unsigned int ip_dst_mtu_maybe_forward(const struct dst_entry *dst,
  336. bool forwarding)
  337. {
  338. struct net *net = dev_net(dst->dev);
  339. unsigned int mtu;
  340. if (net->ipv4.sysctl_ip_fwd_use_pmtu ||
  341. ip_mtu_locked(dst) ||
  342. !forwarding)
  343. return dst_mtu(dst);
  344. /* 'forwarding = true' case should always honour route mtu */
  345. mtu = dst_metric_raw(dst, RTAX_MTU);
  346. if (mtu)
  347. return mtu;
  348. return min(READ_ONCE(dst->dev->mtu), IP_MAX_MTU);
  349. }
  350. static inline unsigned int ip_skb_dst_mtu(struct sock *sk,
  351. const struct sk_buff *skb)
  352. {
  353. if (!sk || !sk_fullsock(sk) || ip_sk_use_pmtu(sk)) {
  354. bool forwarding = IPCB(skb)->flags & IPSKB_FORWARDED;
  355. return ip_dst_mtu_maybe_forward(skb_dst(skb), forwarding);
  356. }
  357. return min(READ_ONCE(skb_dst(skb)->dev->mtu), IP_MAX_MTU);
  358. }
  359. int ip_metrics_convert(struct net *net, struct nlattr *fc_mx, int fc_mx_len,
  360. u32 *metrics);
  361. u32 ip_idents_reserve(u32 hash, int segs);
  362. void __ip_select_ident(struct net *net, struct iphdr *iph, int segs);
  363. static inline void ip_select_ident_segs(struct net *net, struct sk_buff *skb,
  364. struct sock *sk, int segs)
  365. {
  366. struct iphdr *iph = ip_hdr(skb);
  367. if ((iph->frag_off & htons(IP_DF)) && !skb->ignore_df) {
  368. /* This is only to work around buggy Windows95/2000
  369. * VJ compression implementations. If the ID field
  370. * does not change, they drop every other packet in
  371. * a TCP stream using header compression.
  372. */
  373. if (sk && inet_sk(sk)->inet_daddr) {
  374. iph->id = htons(inet_sk(sk)->inet_id);
  375. inet_sk(sk)->inet_id += segs;
  376. } else {
  377. iph->id = 0;
  378. }
  379. } else {
  380. __ip_select_ident(net, iph, segs);
  381. }
  382. }
  383. static inline void ip_select_ident(struct net *net, struct sk_buff *skb,
  384. struct sock *sk)
  385. {
  386. ip_select_ident_segs(net, skb, sk, 1);
  387. }
  388. static inline __wsum inet_compute_pseudo(struct sk_buff *skb, int proto)
  389. {
  390. return csum_tcpudp_nofold(ip_hdr(skb)->saddr, ip_hdr(skb)->daddr,
  391. skb->len, proto, 0);
  392. }
  393. /* copy IPv4 saddr & daddr to flow_keys, possibly using 64bit load/store
  394. * Equivalent to : flow->v4addrs.src = iph->saddr;
  395. * flow->v4addrs.dst = iph->daddr;
  396. */
  397. static inline void iph_to_flow_copy_v4addrs(struct flow_keys *flow,
  398. const struct iphdr *iph)
  399. {
  400. BUILD_BUG_ON(offsetof(typeof(flow->addrs), v4addrs.dst) !=
  401. offsetof(typeof(flow->addrs), v4addrs.src) +
  402. sizeof(flow->addrs.v4addrs.src));
  403. memcpy(&flow->addrs.v4addrs, &iph->saddr, sizeof(flow->addrs.v4addrs));
  404. flow->control.addr_type = FLOW_DISSECTOR_KEY_IPV4_ADDRS;
  405. }
  406. static inline __wsum inet_gro_compute_pseudo(struct sk_buff *skb, int proto)
  407. {
  408. const struct iphdr *iph = skb_gro_network_header(skb);
  409. return csum_tcpudp_nofold(iph->saddr, iph->daddr,
  410. skb_gro_len(skb), proto, 0);
  411. }
  412. /*
  413. * Map a multicast IP onto multicast MAC for type ethernet.
  414. */
  415. static inline void ip_eth_mc_map(__be32 naddr, char *buf)
  416. {
  417. __u32 addr=ntohl(naddr);
  418. buf[0]=0x01;
  419. buf[1]=0x00;
  420. buf[2]=0x5e;
  421. buf[5]=addr&0xFF;
  422. addr>>=8;
  423. buf[4]=addr&0xFF;
  424. addr>>=8;
  425. buf[3]=addr&0x7F;
  426. }
  427. /*
  428. * Map a multicast IP onto multicast MAC for type IP-over-InfiniBand.
  429. * Leave P_Key as 0 to be filled in by driver.
  430. */
  431. static inline void ip_ib_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
  432. {
  433. __u32 addr;
  434. unsigned char scope = broadcast[5] & 0xF;
  435. buf[0] = 0; /* Reserved */
  436. buf[1] = 0xff; /* Multicast QPN */
  437. buf[2] = 0xff;
  438. buf[3] = 0xff;
  439. addr = ntohl(naddr);
  440. buf[4] = 0xff;
  441. buf[5] = 0x10 | scope; /* scope from broadcast address */
  442. buf[6] = 0x40; /* IPv4 signature */
  443. buf[7] = 0x1b;
  444. buf[8] = broadcast[8]; /* P_Key */
  445. buf[9] = broadcast[9];
  446. buf[10] = 0;
  447. buf[11] = 0;
  448. buf[12] = 0;
  449. buf[13] = 0;
  450. buf[14] = 0;
  451. buf[15] = 0;
  452. buf[19] = addr & 0xff;
  453. addr >>= 8;
  454. buf[18] = addr & 0xff;
  455. addr >>= 8;
  456. buf[17] = addr & 0xff;
  457. addr >>= 8;
  458. buf[16] = addr & 0x0f;
  459. }
  460. static inline void ip_ipgre_mc_map(__be32 naddr, const unsigned char *broadcast, char *buf)
  461. {
  462. if ((broadcast[0] | broadcast[1] | broadcast[2] | broadcast[3]) != 0)
  463. memcpy(buf, broadcast, 4);
  464. else
  465. memcpy(buf, &naddr, sizeof(naddr));
  466. }
  467. #if IS_ENABLED(CONFIG_IPV6)
  468. #include <linux/ipv6.h>
  469. #endif
  470. static __inline__ void inet_reset_saddr(struct sock *sk)
  471. {
  472. inet_sk(sk)->inet_rcv_saddr = inet_sk(sk)->inet_saddr = 0;
  473. #if IS_ENABLED(CONFIG_IPV6)
  474. if (sk->sk_family == PF_INET6) {
  475. struct ipv6_pinfo *np = inet6_sk(sk);
  476. memset(&np->saddr, 0, sizeof(np->saddr));
  477. memset(&sk->sk_v6_rcv_saddr, 0, sizeof(sk->sk_v6_rcv_saddr));
  478. }
  479. #endif
  480. }
  481. #endif
  482. static inline unsigned int ipv4_addr_hash(__be32 ip)
  483. {
  484. return (__force unsigned int) ip;
  485. }
  486. static inline u32 ipv4_portaddr_hash(const struct net *net,
  487. __be32 saddr,
  488. unsigned int port)
  489. {
  490. return jhash_1word((__force u32)saddr, net_hash_mix(net)) ^ port;
  491. }
  492. bool ip_call_ra_chain(struct sk_buff *skb);
  493. /*
  494. * Functions provided by ip_fragment.c
  495. */
  496. enum ip_defrag_users {
  497. IP_DEFRAG_LOCAL_DELIVER,
  498. IP_DEFRAG_CALL_RA_CHAIN,
  499. IP_DEFRAG_CONNTRACK_IN,
  500. __IP_DEFRAG_CONNTRACK_IN_END = IP_DEFRAG_CONNTRACK_IN + USHRT_MAX,
  501. IP_DEFRAG_CONNTRACK_OUT,
  502. __IP_DEFRAG_CONNTRACK_OUT_END = IP_DEFRAG_CONNTRACK_OUT + USHRT_MAX,
  503. IP_DEFRAG_CONNTRACK_BRIDGE_IN,
  504. __IP_DEFRAG_CONNTRACK_BRIDGE_IN = IP_DEFRAG_CONNTRACK_BRIDGE_IN + USHRT_MAX,
  505. IP_DEFRAG_VS_IN,
  506. IP_DEFRAG_VS_OUT,
  507. IP_DEFRAG_VS_FWD,
  508. IP_DEFRAG_AF_PACKET,
  509. IP_DEFRAG_MACVLAN,
  510. };
  511. /* Return true if the value of 'user' is between 'lower_bond'
  512. * and 'upper_bond' inclusively.
  513. */
  514. static inline bool ip_defrag_user_in_between(u32 user,
  515. enum ip_defrag_users lower_bond,
  516. enum ip_defrag_users upper_bond)
  517. {
  518. return user >= lower_bond && user <= upper_bond;
  519. }
  520. int ip_defrag(struct net *net, struct sk_buff *skb, u32 user);
  521. #ifdef CONFIG_INET
  522. struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user);
  523. #else
  524. static inline struct sk_buff *ip_check_defrag(struct net *net, struct sk_buff *skb, u32 user)
  525. {
  526. return skb;
  527. }
  528. #endif
  529. /*
  530. * Functions provided by ip_forward.c
  531. */
  532. int ip_forward(struct sk_buff *skb);
  533. /*
  534. * Functions provided by ip_options.c
  535. */
  536. void ip_options_build(struct sk_buff *skb, struct ip_options *opt,
  537. __be32 daddr, struct rtable *rt, int is_frag);
  538. int __ip_options_echo(struct net *net, struct ip_options *dopt,
  539. struct sk_buff *skb, const struct ip_options *sopt);
  540. static inline int ip_options_echo(struct net *net, struct ip_options *dopt,
  541. struct sk_buff *skb)
  542. {
  543. return __ip_options_echo(net, dopt, skb, &IPCB(skb)->opt);
  544. }
  545. void ip_options_fragment(struct sk_buff *skb);
  546. int __ip_options_compile(struct net *net, struct ip_options *opt,
  547. struct sk_buff *skb, __be32 *info);
  548. int ip_options_compile(struct net *net, struct ip_options *opt,
  549. struct sk_buff *skb);
  550. int ip_options_get(struct net *net, struct ip_options_rcu **optp,
  551. unsigned char *data, int optlen);
  552. int ip_options_get_from_user(struct net *net, struct ip_options_rcu **optp,
  553. unsigned char __user *data, int optlen);
  554. void ip_options_undo(struct ip_options *opt);
  555. void ip_forward_options(struct sk_buff *skb);
  556. int ip_options_rcv_srr(struct sk_buff *skb, struct net_device *dev);
  557. /*
  558. * Functions provided by ip_sockglue.c
  559. */
  560. void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb);
  561. void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
  562. struct sk_buff *skb, int tlen, int offset);
  563. int ip_cmsg_send(struct sock *sk, struct msghdr *msg,
  564. struct ipcm_cookie *ipc, bool allow_ipv6);
  565. int ip_setsockopt(struct sock *sk, int level, int optname, char __user *optval,
  566. unsigned int optlen);
  567. int ip_getsockopt(struct sock *sk, int level, int optname, char __user *optval,
  568. int __user *optlen);
  569. int compat_ip_setsockopt(struct sock *sk, int level, int optname,
  570. char __user *optval, unsigned int optlen);
  571. int compat_ip_getsockopt(struct sock *sk, int level, int optname,
  572. char __user *optval, int __user *optlen);
  573. int ip_ra_control(struct sock *sk, unsigned char on,
  574. void (*destructor)(struct sock *));
  575. int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len);
  576. void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, __be16 port,
  577. u32 info, u8 *payload);
  578. void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 dport,
  579. u32 info);
  580. static inline void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
  581. {
  582. ip_cmsg_recv_offset(msg, skb->sk, skb, 0, 0);
  583. }
  584. bool icmp_global_allow(void);
  585. extern int sysctl_icmp_msgs_per_sec;
  586. extern int sysctl_icmp_msgs_burst;
  587. #ifdef CONFIG_PROC_FS
  588. int ip_misc_proc_init(void);
  589. #endif
  590. int rtm_getroute_parse_ip_proto(struct nlattr *attr, u8 *ip_proto, u8 family,
  591. struct netlink_ext_ack *extack);
  592. static inline bool inetdev_valid_mtu(unsigned int mtu)
  593. {
  594. return likely(mtu >= IPV4_MIN_MTU);
  595. }
  596. #endif /* _IP_H */