net6.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Copyright (C) 2013 Allied Telesis Labs NZ
  4. * Chris Packham, <judge.packham@gmail.com>
  5. *
  6. * Copyright (C) 2022 YADRO
  7. * Viacheslav Mitrofanov <v.v.mitrofanov@yadro.com>
  8. */
  9. #ifndef __NET6_H__
  10. #define __NET6_H__
  11. #include <net.h>
  12. #include <linux/ctype.h>
  13. /* struct in6_addr - 128 bits long IPv6 address */
  14. struct in6_addr {
  15. union {
  16. u8 u6_addr8[16];
  17. __be16 u6_addr16[8];
  18. __be32 u6_addr32[4];
  19. } in6_u;
  20. #define s6_addr in6_u.u6_addr8
  21. #define s6_addr16 in6_u.u6_addr16
  22. #define s6_addr32 in6_u.u6_addr32
  23. } __packed;
  24. #define IN6ADDRSZ sizeof(struct in6_addr)
  25. #define INETHADDRSZ sizeof(net_ethaddr)
  26. #define PROT_IP6 0x86DD /* IPv6 protocol */
  27. #define PROT_ICMPV6 58 /* ICMPv6 protocol*/
  28. #define IPV6_ADDRSCOPE_INTF 0x01
  29. #define IPV6_ADDRSCOPE_LINK 0x02
  30. #define IPV6_ADDRSCOPE_AMDIN 0x04
  31. #define IPV6_ADDRSCOPE_SITE 0x05
  32. #define IPV6_ADDRSCOPE_ORG 0x08
  33. #define IPV6_ADDRSCOPE_GLOBAL 0x0E
  34. #define USE_IP6_CMD_PARAM "-ipv6"
  35. /**
  36. * struct ipv6hdr - Internet Protocol V6 (IPv6) header.
  37. *
  38. * IPv6 packet header as defined in RFC 2460.
  39. */
  40. struct ip6_hdr {
  41. #if defined(__LITTLE_ENDIAN_BITFIELD)
  42. u8 priority:4,
  43. version:4;
  44. #elif defined(__BIG_ENDIAN_BITFIELD)
  45. u8 version:4,
  46. priority:4;
  47. #else
  48. #error "Please fix <asm/byteorder.h>"
  49. #endif
  50. u8 flow_lbl[3];
  51. __be16 payload_len;
  52. u8 nexthdr;
  53. u8 hop_limit;
  54. struct in6_addr saddr;
  55. struct in6_addr daddr;
  56. } __packed;
  57. #define IP6_HDR_SIZE (sizeof(struct ip6_hdr))
  58. /* struct udp_hdr - User Datagram Protocol header */
  59. struct udp_hdr {
  60. u16 udp_src; /* UDP source port */
  61. u16 udp_dst; /* UDP destination port */
  62. u16 udp_len; /* Length of UDP packet */
  63. u16 udp_xsum; /* Checksum */
  64. } __packed;
  65. /*
  66. * Handy for static initialisations of struct in6_addr, atlhough the
  67. * c99 '= { 0 }' idiom might work depending on you compiler.
  68. */
  69. #define ZERO_IPV6_ADDR { { { 0x00, 0x00, 0x00, 0x00, \
  70. 0x00, 0x00, 0x00, 0x00, \
  71. 0x00, 0x00, 0x00, 0x00, \
  72. 0x00, 0x00, 0x00, 0x00 } } }
  73. /*
  74. * All-routers multicast address is the link-local scope address to reach all
  75. * routers.
  76. */
  77. #define ALL_ROUTERS_MULT_ADDR { { { 0xFF, 0x02, 0x00, 0x00, \
  78. 0x00, 0x00, 0x00, 0x00, \
  79. 0x00, 0x00, 0x00, 0x00, \
  80. 0x00, 0x00, 0x00, 0x02 } } }
  81. #define IPV6_LINK_LOCAL_PREFIX 0xfe80
  82. #define IPV6_LINK_LOCAL_MASK 0xffb0 /* The first 10-bit of address mask. */
  83. /* hop limit for neighbour discovery packets */
  84. #define IPV6_NDISC_HOPLIMIT 255
  85. #define NDISC_TIMEOUT 5000UL
  86. #define NDISC_TIMEOUT_COUNT 3
  87. /* struct icmp6hdr - Internet Control Message Protocol header for IPV6 */
  88. struct icmp6hdr {
  89. u8 icmp6_type;
  90. #define IPV6_ICMP_ECHO_REQUEST 128
  91. #define IPV6_ICMP_ECHO_REPLY 129
  92. #define IPV6_NDISC_ROUTER_SOLICITATION 133
  93. #define IPV6_NDISC_ROUTER_ADVERTISEMENT 134
  94. #define IPV6_NDISC_NEIGHBOUR_SOLICITATION 135
  95. #define IPV6_NDISC_NEIGHBOUR_ADVERTISEMENT 136
  96. #define IPV6_NDISC_REDIRECT 137
  97. u8 icmp6_code;
  98. __be16 icmp6_cksum;
  99. /* ICMPv6 data */
  100. union {
  101. __be32 un_data32[1];
  102. __be16 un_data16[2];
  103. u8 un_data8[4];
  104. /* struct icmpv6_echo - echo request/reply message format */
  105. struct icmpv6_echo {
  106. __be16 identifier;
  107. __be16 sequence;
  108. } u_echo;
  109. /* struct icmpv6_nd_advt - Neighbor Advertisement format */
  110. struct icmpv6_nd_advt {
  111. #if defined(__LITTLE_ENDIAN_BITFIELD)
  112. __be32 reserved:5,
  113. override:1,
  114. solicited:1,
  115. router:1,
  116. reserved2:24;
  117. #elif defined(__BIG_ENDIAN_BITFIELD)
  118. __be32 router:1,
  119. solicited:1,
  120. override:1,
  121. reserved:29;
  122. #else
  123. #error "Please fix <asm/byteorder.h>"
  124. #endif
  125. } u_nd_advt;
  126. /* struct icmpv6_nd_ra - Router Advertisement format */
  127. struct icmpv6_nd_ra {
  128. u8 hop_limit;
  129. #if defined(__LITTLE_ENDIAN_BITFIELD)
  130. u8 reserved:6,
  131. other:1,
  132. managed:1;
  133. #elif defined(__BIG_ENDIAN_BITFIELD)
  134. u8 managed:1,
  135. other:1,
  136. reserved:6;
  137. #else
  138. #error "Please fix <asm/byteorder.h>"
  139. #endif
  140. __be16 rt_lifetime;
  141. } u_nd_ra;
  142. } icmp6_dataun;
  143. #define icmp6_identifier icmp6_dataun.u_echo.identifier
  144. #define icmp6_sequence icmp6_dataun.u_echo.sequence
  145. #define icmp6_pointer icmp6_dataun.un_data32[0]
  146. #define icmp6_mtu icmp6_dataun.un_data32[0]
  147. #define icmp6_unused icmp6_dataun.un_data32[0]
  148. #define icmp6_maxdelay icmp6_dataun.un_data16[0]
  149. #define icmp6_router icmp6_dataun.u_nd_advt.router
  150. #define icmp6_solicited icmp6_dataun.u_nd_advt.solicited
  151. #define icmp6_override icmp6_dataun.u_nd_advt.override
  152. #define icmp6_ndiscreserved icmp6_dataun.u_nd_advt.reserved
  153. #define icmp6_hop_limit icmp6_dataun.u_nd_ra.hop_limit
  154. #define icmp6_addrconf_managed icmp6_dataun.u_nd_ra.managed
  155. #define icmp6_addrconf_other icmp6_dataun.u_nd_ra.other
  156. #define icmp6_rt_lifetime icmp6_dataun.u_nd_ra.rt_lifetime
  157. } __packed;
  158. /*
  159. * struct icmp6_ra_prefix_info - Prefix Information option of the ICMPv6 message
  160. * The Prefix Information option provides hosts with on-link prefixes and
  161. * prefixes for Address Autoconfiguration. Refer to RFC 4861 for more info.
  162. */
  163. struct icmp6_ra_prefix_info {
  164. u8 type; /* Type is 3 for Prefix Information. */
  165. u8 len; /* Len is 4 for Prefix Information. */
  166. /* The number of leading bits in the Prefix that are valid. */
  167. u8 prefix_len;
  168. u8 reserved1:6, /* MUST be ignored by the receiver. */
  169. aac:1, /* autonomous address-configuration flag */
  170. /* Indicates that this prefix can be used for on-link determination. */
  171. on_link:1;
  172. /*
  173. * The length of time in seconds that the prefix is valid for the
  174. * purpose of on-link determination.
  175. */
  176. __be32 valid_lifetime;
  177. /* The length of time addresses remain preferred. */
  178. __be32 preferred_lifetime;
  179. __be32 reserved2; /* MUST be ignored by the receiver. */
  180. /*
  181. * Prefix is an IP address or a prefix of an IP address. The Prefix
  182. * Length field contains the number of valid leading bits in the prefix.
  183. * The bits in the prefix after the prefix length are reserved and MUST
  184. * be initialized to zero by the sender and ignored by the receiver.
  185. */
  186. struct in6_addr prefix;
  187. } __packed;
  188. extern struct in6_addr const net_null_addr_ip6; /* NULL IPv6 address */
  189. extern struct in6_addr net_gateway6; /* Our gateways IPv6 address */
  190. extern struct in6_addr net_ip6; /* Our IPv6 addr (0 = unknown) */
  191. extern struct in6_addr net_link_local_ip6; /* Our link local IPv6 addr */
  192. extern u32 net_prefix_length; /* Our prefixlength (0 = unknown) */
  193. extern struct in6_addr net_server_ip6; /* Server IPv6 addr (0 = unknown) */
  194. extern struct in6_addr net_ping_ip6; /* the ipv6 address to ping */
  195. extern bool use_ip6;
  196. #if IS_ENABLED(CONFIG_IPV6)
  197. /**
  198. * string_to_ip6() - Convert IPv6 string addr to inner IPV6 addr format
  199. *
  200. * Examples of valid strings:
  201. * 2001:db8::0:1234:1
  202. * 2001:0db8:0000:0000:0000:0000:1234:0001
  203. * ::1
  204. * ::ffff:192.168.1.1
  205. *
  206. * Examples of invalid strings
  207. * 2001:db8::0::0 (:: can only appear once)
  208. * 2001:db8:192.168.1.1::1 (v4 part can only appear at the end)
  209. * 192.168.1.1 (we don't implicity map v4)
  210. *
  211. * @s: IPv6 string addr format
  212. * @len: IPv6 string addr length
  213. * @addr: converted IPv6 addr
  214. * Return: 0 if conversion successful, -EINVAL if fail
  215. */
  216. int string_to_ip6(const char *s, size_t len, struct in6_addr *addr);
  217. /**
  218. * ip6_is_unspecified_addr() - Check if IPv6 addr is not set i.e. is zero
  219. *
  220. * @addr: IPv6 addr
  221. * Return: 0 if addr is not set, -1 if is set
  222. */
  223. int ip6_is_unspecified_addr(struct in6_addr *addr);
  224. /**
  225. * ip6_is_our_addr() - Check if IPv6 addr belongs to our host addr
  226. *
  227. * We have 2 addresses that we should respond to. A link local address and a
  228. * global address. This returns true if the specified address matches either
  229. * of these.
  230. *
  231. * @addr: addr to check
  232. * Return: 0 if addr is our, -1 otherwise
  233. */
  234. int ip6_is_our_addr(struct in6_addr *addr);
  235. /**
  236. * ip6_addr_in_subnet() - Check if two IPv6 addresses are in the same subnet
  237. *
  238. * @our_addr: first IPv6 addr
  239. * @neigh_addr: second IPv6 addr
  240. * @prefix_length: network mask length
  241. * Return: 0 if two addresses in the same subnet, -1 otherwise
  242. */
  243. int ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr,
  244. u32 prefix_length);
  245. /**
  246. * ip6_make_lladd() - rMake up IPv6 Link Local address
  247. *
  248. * @lladdr: formed IPv6 Link Local address
  249. * @enetaddr: MAC addr of a device
  250. */
  251. void ip6_make_lladdr(struct in6_addr *lladr, unsigned char const enetaddr[6]);
  252. /**
  253. * ip6_make_snma() - aMake up Solicited Node Multicast Address from IPv6 addr
  254. *
  255. * @mcast_addr: formed SNMA addr
  256. * @ip6_addr: base IPv6 addr
  257. */
  258. void ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr);
  259. /**
  260. * ip6_make_mult_ethdstaddr() - Make up IPv6 multicast addr
  261. *
  262. * @enetaddr: MAC addr of a device
  263. * @mcast_addr: formed IPv6 multicast addr
  264. */
  265. void ip6_make_mult_ethdstaddr(unsigned char enetaddr[6],
  266. struct in6_addr *mcast_addr);
  267. /**
  268. * csum_partial() - Compute an internet checksum
  269. *
  270. * @buff: buffer to be checksummed
  271. * @len: length of buffer
  272. * @sum: initial sum to be added in
  273. * Return: internet checksum of the buffer
  274. */
  275. unsigned int csum_partial(const unsigned char *buff, int len, unsigned int sum);
  276. /**
  277. * csum_ipv6_magic() - Compute checksum of IPv6 "psuedo-header" per RFC2460 section 8.1
  278. *
  279. * @saddr: source IPv6 addr
  280. * @daddr: destination IPv6 add
  281. * @len: data length to be checksummed
  282. * @proto: IPv6 above protocol code
  283. * @csum: upper layer checksum
  284. * Return: computed checksum
  285. */
  286. unsigned short int csum_ipv6_magic(struct in6_addr *saddr,
  287. struct in6_addr *daddr, u16 len,
  288. unsigned short proto, unsigned int csum);
  289. /**
  290. * ip6_add_hdr() - Make up IPv6 header
  291. *
  292. * @xip: pointer to IPv6 header to be formed
  293. * @src: source IPv6 addr
  294. * @dest: destination IPv6 addr
  295. * @nextheader: next header type
  296. * @hoplimit: hop limit
  297. * @payload_len: payload length
  298. * Return: IPv6 header length
  299. */
  300. int ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
  301. int nextheader, int hoplimit, int payload_len);
  302. /**
  303. * net_send_udp_packet6() - Make up UDP packet and send it
  304. *
  305. * @ether: destination MAC addr
  306. * @dest: destination IPv6 addr
  307. * @dport: destination port
  308. * @sport: source port
  309. * @len: UDP packet length
  310. * Return: 0 if send successfully, -1 otherwise
  311. */
  312. int net_send_udp_packet6(uchar *ether, struct in6_addr *dest, int dport,
  313. int sport, int len);
  314. /**
  315. * net_ip6_handler() - Handle IPv6 packet
  316. *
  317. * @et: pointer to the beginning of the packet
  318. * @ip6: pointer to the beginning of IPv6 protocol
  319. * @len: incoming packet len
  320. * Return: 0 if handle packet successfully, -EINVAL in case of invalid protocol
  321. */
  322. int net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len);
  323. /**
  324. * net_copy_ip6() - Copy IPv6 addr
  325. *
  326. * @to: destination IPv6 addr
  327. * @from: source IPv6 addr
  328. */
  329. static inline void net_copy_ip6(void *to, const void *from)
  330. {
  331. memcpy((void *)to, from, sizeof(struct in6_addr));
  332. }
  333. #else
  334. static inline int
  335. string_to_ip6(const char *s, size_t len, struct in6_addr *addr)
  336. {
  337. return -EINVAL;
  338. }
  339. static inline int ip6_is_unspecified_addr(struct in6_addr *addr)
  340. {
  341. return -1;
  342. }
  343. static inline int ip6_is_our_addr(struct in6_addr *addr)
  344. {
  345. return -1;
  346. }
  347. static inline int
  348. ip6_addr_in_subnet(struct in6_addr *our_addr, struct in6_addr *neigh_addr,
  349. u32 prefix_length)
  350. {
  351. return -1;
  352. }
  353. static inline void
  354. ip6_make_lladdr(struct in6_addr *lladdr, unsigned char const enetaddr[6])
  355. {
  356. }
  357. static inline void
  358. ip6_make_snma(struct in6_addr *mcast_addr, struct in6_addr *ip6_addr)
  359. {
  360. }
  361. static inline void
  362. ip6_make_mult_ethdstaddr(unsigned char enetaddr[6],
  363. struct in6_addr *mcast_addr)
  364. {
  365. }
  366. static inline unsigned int
  367. csum_partial(const unsigned char *buff, int len, unsigned int sum)
  368. {
  369. return 0;
  370. }
  371. static inline unsigned short
  372. csum_ipv6_magic(struct in6_addr *saddr,
  373. struct in6_addr *daddr, u16 len,
  374. unsigned short proto, unsigned int csum)
  375. {
  376. return 0;
  377. }
  378. static inline unsigned int
  379. ip6_add_hdr(uchar *xip, struct in6_addr *src, struct in6_addr *dest,
  380. int nextheader, int hoplimit, int payload_len)
  381. {
  382. return 0;
  383. }
  384. static inline int
  385. net_send_udp_packet6(uchar *ether, struct in6_addr *dest,
  386. int dport, int sport, int len)
  387. {
  388. return -1;
  389. }
  390. static inline int
  391. net_ip6_handler(struct ethernet_hdr *et, struct ip6_hdr *ip6,
  392. int len)
  393. {
  394. return -EINVAL;
  395. }
  396. static inline void net_copy_ip6(void *to, const void *from)
  397. {
  398. }
  399. #endif
  400. #if IS_ENABLED(CONFIG_CMD_PING6)
  401. /* Send ping requset */
  402. void ping6_start(void);
  403. /**
  404. * ping6_receive() - Handle reception of ICMPv6 echo request/reply
  405. *
  406. * @et: pointer to incoming patcket
  407. * @ip6: pointer to IPv6 protocol
  408. * @len: packet length
  409. * Return: 0 if success, -EINVAL in case of failure during reception
  410. */
  411. int ping6_receive(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len);
  412. #else
  413. static inline void ping6_start(void)
  414. {
  415. }
  416. static inline
  417. int ping6_receive(struct ethernet_hdr *et, struct ip6_hdr *ip6, int len)
  418. {
  419. return -EINVAL;
  420. }
  421. #endif /* CONFIG_CMD_PING6 */
  422. #endif /* __NET6_H__ */