900-unaligned_access_hacks.patch 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  1. From: Felix Fietkau <[email protected]>
  2. Subject: [PATCH] ar71xx: fix unaligned access in a few more places
  3. SVN-Revision: 35130
  4. ---
  5. arch/mips/include/asm/checksum.h | 83 +++---------------
  6. include/uapi/linux/ip.h | 2 +-
  7. include/uapi/linux/ipv6.h | 2 +-
  8. include/uapi/linux/tcp.h | 4 ++--
  9. include/uapi/linux/udp.h | 2 +-
  10. net/netfilter/nf_conntrack_core.c | 4 ++--
  11. include/uapi/linux/icmp.h | 2 +-
  12. include/uapi/linux/in6.h | 2 +-
  13. net/ipv6/tcp_ipv6.c | 9 +++--
  14. net/ipv6/datagram.c | 6 ++--
  15. net/ipv6/exthdrs.c | 2 +-
  16. include/linux/types.h | 5 +++
  17. net/ipv4/af_inet.c | 4 ++--
  18. net/ipv4/tcp_output.c | 69 +++++++++--------
  19. include/uapi/linux/igmp.h | 8 +++---
  20. net/core/flow_dissector.c | 2 +-
  21. include/uapi/linux/icmpv6.h | 2 +-
  22. include/net/ndisc.h | 10 ++++----
  23. net/sched/cls_u32.c | 6 +++---
  24. net/ipv6/ip6_offload.c | 2 +-
  25. include/net/addrconf.h | 2 +-
  26. include/net/inet_ecn.h | 4 ++--
  27. include/net/ipv6.h | 23 +++++----
  28. include/net/secure_seq.h | 1 +
  29. include/uapi/linux/in.h | 2 +-
  30. net/ipv6/ip6_fib.h | 2 +-
  31. net/netfilter/nf_conntrack_proto_tcp.c | 2 +-
  32. net/xfrm/xfrm_input.c | 4 ++--
  33. net/ipv4/tcp_input.c | 12 ++++---
  34. include/uapi/linux/if_pppox.h | 1 +
  35. net/ipv6/netfilter/nf_log_ipv6.c | 4 ++--
  36. include/net/neighbour.h | 6 +++--
  37. include/uapi/linux/netfilter_arp/arp_tables.h | 2 +-
  38. net/core/utils.c | 10 +++++--
  39. include/linux/etherdevice.h | 11 ++++---
  40. net/ipv4/tcp_offload.c | 6 +++---
  41. net/ipv6/netfilter/ip6table_mangle.c | 4 ++--
  42. 37 file changed, 171 insertions(+), 141 deletions(-)
  43. --- a/arch/mips/include/asm/checksum.h
  44. +++ b/arch/mips/include/asm/checksum.h
  45. @@ -100,26 +100,30 @@ static inline __sum16 ip_fast_csum(const
  46. const unsigned int *stop = word + ihl;
  47. unsigned int csum;
  48. int carry;
  49. + unsigned int w;
  50. - csum = word[0];
  51. - csum += word[1];
  52. - carry = (csum < word[1]);
  53. + csum = net_hdr_word(word++);
  54. +
  55. + w = net_hdr_word(word++);
  56. + csum += w;
  57. + carry = (csum < w);
  58. csum += carry;
  59. - csum += word[2];
  60. - carry = (csum < word[2]);
  61. + w = net_hdr_word(word++);
  62. + csum += w;
  63. + carry = (csum < w);
  64. csum += carry;
  65. - csum += word[3];
  66. - carry = (csum < word[3]);
  67. + w = net_hdr_word(word++);
  68. + csum += w;
  69. + carry = (csum < w);
  70. csum += carry;
  71. - word += 4;
  72. do {
  73. - csum += *word;
  74. - carry = (csum < *word);
  75. + w = net_hdr_word(word++);
  76. + csum += w;
  77. + carry = (csum < w);
  78. csum += carry;
  79. - word++;
  80. } while (word != stop);
  81. return csum_fold(csum);
  82. @@ -179,73 +183,6 @@ static inline __sum16 ip_compute_csum(co
  83. return csum_fold(csum_partial(buff, len, 0));
  84. }
  85. -#define _HAVE_ARCH_IPV6_CSUM
  86. -static __inline__ __sum16 csum_ipv6_magic(const struct in6_addr *saddr,
  87. - const struct in6_addr *daddr,
  88. - __u32 len, __u8 proto,
  89. - __wsum sum)
  90. -{
  91. - __wsum tmp;
  92. -
  93. - __asm__(
  94. - " .set push # csum_ipv6_magic\n"
  95. - " .set noreorder \n"
  96. - " .set noat \n"
  97. - " addu %0, %5 # proto (long in network byte order)\n"
  98. - " sltu $1, %0, %5 \n"
  99. - " addu %0, $1 \n"
  100. -
  101. - " addu %0, %6 # csum\n"
  102. - " sltu $1, %0, %6 \n"
  103. - " lw %1, 0(%2) # four words source address\n"
  104. - " addu %0, $1 \n"
  105. - " addu %0, %1 \n"
  106. - " sltu $1, %0, %1 \n"
  107. -
  108. - " lw %1, 4(%2) \n"
  109. - " addu %0, $1 \n"
  110. - " addu %0, %1 \n"
  111. - " sltu $1, %0, %1 \n"
  112. -
  113. - " lw %1, 8(%2) \n"
  114. - " addu %0, $1 \n"
  115. - " addu %0, %1 \n"
  116. - " sltu $1, %0, %1 \n"
  117. -
  118. - " lw %1, 12(%2) \n"
  119. - " addu %0, $1 \n"
  120. - " addu %0, %1 \n"
  121. - " sltu $1, %0, %1 \n"
  122. -
  123. - " lw %1, 0(%3) \n"
  124. - " addu %0, $1 \n"
  125. - " addu %0, %1 \n"
  126. - " sltu $1, %0, %1 \n"
  127. -
  128. - " lw %1, 4(%3) \n"
  129. - " addu %0, $1 \n"
  130. - " addu %0, %1 \n"
  131. - " sltu $1, %0, %1 \n"
  132. -
  133. - " lw %1, 8(%3) \n"
  134. - " addu %0, $1 \n"
  135. - " addu %0, %1 \n"
  136. - " sltu $1, %0, %1 \n"
  137. -
  138. - " lw %1, 12(%3) \n"
  139. - " addu %0, $1 \n"
  140. - " addu %0, %1 \n"
  141. - " sltu $1, %0, %1 \n"
  142. -
  143. - " addu %0, $1 # Add final carry\n"
  144. - " .set pop"
  145. - : "=&r" (sum), "=&r" (tmp)
  146. - : "r" (saddr), "r" (daddr),
  147. - "0" (htonl(len)), "r" (htonl(proto)), "r" (sum));
  148. -
  149. - return csum_fold(sum);
  150. -}
  151. -
  152. #include <asm-generic/checksum.h>
  153. #endif /* CONFIG_GENERIC_CSUM */
  154. --- a/include/uapi/linux/ip.h
  155. +++ b/include/uapi/linux/ip.h
  156. @@ -106,7 +106,7 @@ struct iphdr {
  157. __be32 daddr;
  158. );
  159. /*The options start here. */
  160. -};
  161. +} __attribute__((packed, aligned(2)));
  162. struct ip_auth_hdr {
  163. --- a/include/uapi/linux/ipv6.h
  164. +++ b/include/uapi/linux/ipv6.h
  165. @@ -135,7 +135,7 @@ struct ipv6hdr {
  166. struct in6_addr saddr;
  167. struct in6_addr daddr;
  168. );
  169. -};
  170. +} __attribute__((packed, aligned(2)));
  171. /* index values for the variables in ipv6_devconf */
  172. --- a/include/uapi/linux/tcp.h
  173. +++ b/include/uapi/linux/tcp.h
  174. @@ -55,7 +55,7 @@ struct tcphdr {
  175. __be16 window;
  176. __sum16 check;
  177. __be16 urg_ptr;
  178. -};
  179. +} __attribute__((packed, aligned(2)));
  180. /*
  181. * The union cast uses a gcc extension to avoid aliasing problems
  182. @@ -65,7 +65,7 @@ struct tcphdr {
  183. union tcp_word_hdr {
  184. struct tcphdr hdr;
  185. __be32 words[5];
  186. -};
  187. +} __attribute__((packed, aligned(2)));
  188. #define tcp_flag_word(tp) (((union tcp_word_hdr *)(tp))->words[3])
  189. --- a/include/uapi/linux/udp.h
  190. +++ b/include/uapi/linux/udp.h
  191. @@ -25,7 +25,7 @@ struct udphdr {
  192. __be16 dest;
  193. __be16 len;
  194. __sum16 check;
  195. -};
  196. +} __attribute__((packed, aligned(2)));
  197. /* UDP socket options */
  198. #define UDP_CORK 1 /* Never send partially complete segments */
  199. --- a/net/netfilter/nf_conntrack_core.c
  200. +++ b/net/netfilter/nf_conntrack_core.c
  201. @@ -308,8 +308,8 @@ nf_ct_get_tuple(const struct sk_buff *sk
  202. switch (l3num) {
  203. case NFPROTO_IPV4:
  204. - tuple->src.u3.ip = ap[0];
  205. - tuple->dst.u3.ip = ap[1];
  206. + tuple->src.u3.ip = net_hdr_word(ap++);
  207. + tuple->dst.u3.ip = net_hdr_word(ap);
  208. break;
  209. case NFPROTO_IPV6:
  210. memcpy(tuple->src.u3.ip6, ap, sizeof(tuple->src.u3.ip6));
  211. --- a/include/uapi/linux/icmp.h
  212. +++ b/include/uapi/linux/icmp.h
  213. @@ -102,7 +102,7 @@ struct icmphdr {
  214. } frag;
  215. __u8 reserved[4];
  216. } un;
  217. -};
  218. +} __attribute__((packed, aligned(2)));
  219. /*
  220. --- a/include/uapi/linux/in6.h
  221. +++ b/include/uapi/linux/in6.h
  222. @@ -43,7 +43,7 @@ struct in6_addr {
  223. #define s6_addr16 in6_u.u6_addr16
  224. #define s6_addr32 in6_u.u6_addr32
  225. #endif
  226. -};
  227. +} __attribute__((packed, aligned(2)));
  228. #endif /* __UAPI_DEF_IN6_ADDR */
  229. #if __UAPI_DEF_SOCKADDR_IN6
  230. --- a/net/ipv6/tcp_ipv6.c
  231. +++ b/net/ipv6/tcp_ipv6.c
  232. @@ -35,6 +35,7 @@
  233. #include <linux/ipsec.h>
  234. #include <linux/times.h>
  235. #include <linux/slab.h>
  236. +#include <asm/unaligned.h>
  237. #include <linux/uaccess.h>
  238. #include <linux/ipv6.h>
  239. #include <linux/icmpv6.h>
  240. @@ -901,10 +902,10 @@ static void tcp_v6_send_response(const s
  241. topt = (__be32 *)(t1 + 1);
  242. if (tsecr) {
  243. - *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  244. - (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
  245. - *topt++ = htonl(tsval);
  246. - *topt++ = htonl(tsecr);
  247. + put_unaligned_be32((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  248. + (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP, topt++);
  249. + put_unaligned_be32(tsval, topt++);
  250. + put_unaligned_be32(tsecr, topt++);
  251. }
  252. if (mrst)
  253. --- a/include/linux/ipv6.h
  254. +++ b/include/linux/ipv6.h
  255. @@ -6,6 +6,7 @@
  256. #define ipv6_optlen(p) (((p)->hdrlen+1) << 3)
  257. #define ipv6_authlen(p) (((p)->hdrlen+2) << 2)
  258. +
  259. /*
  260. * This structure contains configuration options per IPv6 link.
  261. */
  262. --- a/net/ipv6/datagram.c
  263. +++ b/net/ipv6/datagram.c
  264. @@ -497,7 +497,7 @@ int ipv6_recv_error(struct sock *sk, str
  265. ipv6_iface_scope_id(&sin->sin6_addr,
  266. IP6CB(skb)->iif);
  267. } else {
  268. - ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
  269. + ipv6_addr_set_v4mapped(net_hdr_word(nh + serr->addr_offset),
  270. &sin->sin6_addr);
  271. sin->sin6_scope_id = 0;
  272. }
  273. @@ -851,12 +851,12 @@ int ip6_datagram_send_ctl(struct net *ne
  274. }
  275. if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
  276. - if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
  277. + if ((fl6->flowlabel^net_hdr_word(CMSG_DATA(cmsg)))&~IPV6_FLOWINFO_MASK) {
  278. err = -EINVAL;
  279. goto exit_f;
  280. }
  281. }
  282. - fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
  283. + fl6->flowlabel = IPV6_FLOWINFO_MASK & net_hdr_word(CMSG_DATA(cmsg));
  284. break;
  285. case IPV6_2292HOPOPTS:
  286. --- a/net/ipv6/exthdrs.c
  287. +++ b/net/ipv6/exthdrs.c
  288. @@ -1003,7 +1003,7 @@ static bool ipv6_hop_jumbo(struct sk_buf
  289. goto drop;
  290. }
  291. - pkt_len = ntohl(*(__be32 *)(nh + optoff + 2));
  292. + pkt_len = ntohl(net_hdr_word(nh + optoff + 2));
  293. if (pkt_len <= IPV6_MAXPLEN) {
  294. icmpv6_param_prob_reason(skb, ICMPV6_HDR_FIELD, optoff + 2,
  295. SKB_DROP_REASON_IP_INHDR);
  296. --- a/include/linux/types.h
  297. +++ b/include/linux/types.h
  298. @@ -232,5 +232,11 @@ typedef void (*swap_func_t)(void *a, voi
  299. typedef int (*cmp_r_func_t)(const void *a, const void *b, const void *priv);
  300. typedef int (*cmp_func_t)(const void *a, const void *b);
  301. +struct net_hdr_word {
  302. + u32 words[1];
  303. +} __attribute__((packed, aligned(2)));
  304. +
  305. +#define net_hdr_word(_p) (((struct net_hdr_word *) (_p))->words[0])
  306. +
  307. #endif /* __ASSEMBLY__ */
  308. #endif /* _LINUX_TYPES_H */
  309. --- a/net/ipv4/af_inet.c
  310. +++ b/net/ipv4/af_inet.c
  311. @@ -1512,8 +1512,8 @@ struct sk_buff *inet_gro_receive(struct
  312. if (unlikely(ip_fast_csum((u8 *)iph, 5)))
  313. goto out;
  314. - id = ntohl(*(__be32 *)&iph->id);
  315. - flush = (u16)((ntohl(*(__be32 *)iph) ^ skb_gro_len(skb)) | (id & ~IP_DF));
  316. + id = ntohl(net_hdr_word(&iph->id));
  317. + flush = (u16)((ntohl(net_hdr_word(iph)) ^ skb_gro_len(skb)) | (id & ~IP_DF));
  318. id >>= 16;
  319. list_for_each_entry(p, head, list) {
  320. --- a/net/ipv4/tcp_output.c
  321. +++ b/net/ipv4/tcp_output.c
  322. @@ -612,48 +612,53 @@ static void tcp_options_write(struct tcp
  323. u16 options = opts->options; /* mungable copy */
  324. if (unlikely(OPTION_MD5 & options)) {
  325. - *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  326. - (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
  327. + net_hdr_word(ptr++) =
  328. + htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  329. + (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
  330. /* overload cookie hash location */
  331. opts->hash_location = (__u8 *)ptr;
  332. ptr += 4;
  333. }
  334. if (unlikely(opts->mss)) {
  335. - *ptr++ = htonl((TCPOPT_MSS << 24) |
  336. - (TCPOLEN_MSS << 16) |
  337. - opts->mss);
  338. + net_hdr_word(ptr++) =
  339. + htonl((TCPOPT_MSS << 24) | (TCPOLEN_MSS << 16) |
  340. + opts->mss);
  341. }
  342. if (likely(OPTION_TS & options)) {
  343. if (unlikely(OPTION_SACK_ADVERTISE & options)) {
  344. - *ptr++ = htonl((TCPOPT_SACK_PERM << 24) |
  345. - (TCPOLEN_SACK_PERM << 16) |
  346. - (TCPOPT_TIMESTAMP << 8) |
  347. - TCPOLEN_TIMESTAMP);
  348. + net_hdr_word(ptr++) =
  349. + htonl((TCPOPT_SACK_PERM << 24) |
  350. + (TCPOLEN_SACK_PERM << 16) |
  351. + (TCPOPT_TIMESTAMP << 8) |
  352. + TCPOLEN_TIMESTAMP);
  353. options &= ~OPTION_SACK_ADVERTISE;
  354. } else {
  355. - *ptr++ = htonl((TCPOPT_NOP << 24) |
  356. - (TCPOPT_NOP << 16) |
  357. - (TCPOPT_TIMESTAMP << 8) |
  358. - TCPOLEN_TIMESTAMP);
  359. + net_hdr_word(ptr++) =
  360. + htonl((TCPOPT_NOP << 24) |
  361. + (TCPOPT_NOP << 16) |
  362. + (TCPOPT_TIMESTAMP << 8) |
  363. + TCPOLEN_TIMESTAMP);
  364. }
  365. - *ptr++ = htonl(opts->tsval);
  366. - *ptr++ = htonl(opts->tsecr);
  367. + net_hdr_word(ptr++) = htonl(opts->tsval);
  368. + net_hdr_word(ptr++) = htonl(opts->tsecr);
  369. }
  370. if (unlikely(OPTION_SACK_ADVERTISE & options)) {
  371. - *ptr++ = htonl((TCPOPT_NOP << 24) |
  372. - (TCPOPT_NOP << 16) |
  373. - (TCPOPT_SACK_PERM << 8) |
  374. - TCPOLEN_SACK_PERM);
  375. + net_hdr_word(ptr++) =
  376. + htonl((TCPOPT_NOP << 24) |
  377. + (TCPOPT_NOP << 16) |
  378. + (TCPOPT_SACK_PERM << 8) |
  379. + TCPOLEN_SACK_PERM);
  380. }
  381. if (unlikely(OPTION_WSCALE & options)) {
  382. - *ptr++ = htonl((TCPOPT_NOP << 24) |
  383. - (TCPOPT_WINDOW << 16) |
  384. - (TCPOLEN_WINDOW << 8) |
  385. - opts->ws);
  386. + net_hdr_word(ptr++) =
  387. + htonl((TCPOPT_NOP << 24) |
  388. + (TCPOPT_WINDOW << 16) |
  389. + (TCPOLEN_WINDOW << 8) |
  390. + opts->ws);
  391. }
  392. if (unlikely(opts->num_sack_blocks)) {
  393. @@ -661,16 +666,17 @@ static void tcp_options_write(struct tcp
  394. tp->duplicate_sack : tp->selective_acks;
  395. int this_sack;
  396. - *ptr++ = htonl((TCPOPT_NOP << 24) |
  397. - (TCPOPT_NOP << 16) |
  398. - (TCPOPT_SACK << 8) |
  399. - (TCPOLEN_SACK_BASE + (opts->num_sack_blocks *
  400. + net_hdr_word(ptr++) =
  401. + htonl((TCPOPT_NOP << 24) |
  402. + (TCPOPT_NOP << 16) |
  403. + (TCPOPT_SACK << 8) |
  404. + (TCPOLEN_SACK_BASE + (opts->num_sack_blocks *
  405. TCPOLEN_SACK_PERBLOCK)));
  406. for (this_sack = 0; this_sack < opts->num_sack_blocks;
  407. ++this_sack) {
  408. - *ptr++ = htonl(sp[this_sack].start_seq);
  409. - *ptr++ = htonl(sp[this_sack].end_seq);
  410. + net_hdr_word(ptr++) = htonl(sp[this_sack].start_seq);
  411. + net_hdr_word(ptr++) = htonl(sp[this_sack].end_seq);
  412. }
  413. tp->rx_opt.dsack = 0;
  414. @@ -683,13 +689,14 @@ static void tcp_options_write(struct tcp
  415. if (foc->exp) {
  416. len = TCPOLEN_EXP_FASTOPEN_BASE + foc->len;
  417. - *ptr = htonl((TCPOPT_EXP << 24) | (len << 16) |
  418. + net_hdr_word(ptr) =
  419. + htonl((TCPOPT_EXP << 24) | (len << 16) |
  420. TCPOPT_FASTOPEN_MAGIC);
  421. p += TCPOLEN_EXP_FASTOPEN_BASE;
  422. } else {
  423. len = TCPOLEN_FASTOPEN_BASE + foc->len;
  424. - *p++ = TCPOPT_FASTOPEN;
  425. - *p++ = len;
  426. + net_hdr_word(p++) = TCPOPT_FASTOPEN;
  427. + net_hdr_word(p++) = len;
  428. }
  429. memcpy(p, foc->val, foc->len);
  430. --- a/include/uapi/linux/igmp.h
  431. +++ b/include/uapi/linux/igmp.h
  432. @@ -33,7 +33,7 @@ struct igmphdr {
  433. __u8 code; /* For newer IGMP */
  434. __sum16 csum;
  435. __be32 group;
  436. -};
  437. +} __attribute__((packed, aligned(2)));
  438. /* V3 group record types [grec_type] */
  439. #define IGMPV3_MODE_IS_INCLUDE 1
  440. @@ -49,7 +49,7 @@ struct igmpv3_grec {
  441. __be16 grec_nsrcs;
  442. __be32 grec_mca;
  443. __be32 grec_src[];
  444. -};
  445. +} __attribute__((packed, aligned(2)));
  446. struct igmpv3_report {
  447. __u8 type;
  448. @@ -58,7 +58,7 @@ struct igmpv3_report {
  449. __be16 resv2;
  450. __be16 ngrec;
  451. struct igmpv3_grec grec[];
  452. -};
  453. +} __attribute__((packed, aligned(2)));
  454. struct igmpv3_query {
  455. __u8 type;
  456. @@ -79,7 +79,7 @@ struct igmpv3_query {
  457. __u8 qqic;
  458. __be16 nsrcs;
  459. __be32 srcs[];
  460. -};
  461. +} __attribute__((packed, aligned(2)));
  462. #define IGMP_HOST_MEMBERSHIP_QUERY 0x11 /* From RFC1112 */
  463. #define IGMP_HOST_MEMBERSHIP_REPORT 0x12 /* Ditto */
  464. --- a/net/core/flow_dissector.c
  465. +++ b/net/core/flow_dissector.c
  466. @@ -131,7 +131,7 @@ __be32 __skb_flow_get_ports(const struct
  467. ports = __skb_header_pointer(skb, thoff + poff,
  468. sizeof(_ports), data, hlen, &_ports);
  469. if (ports)
  470. - return *ports;
  471. + return (__be32)net_hdr_word(ports);
  472. }
  473. return 0;
  474. --- a/include/uapi/linux/icmpv6.h
  475. +++ b/include/uapi/linux/icmpv6.h
  476. @@ -78,7 +78,7 @@ struct icmp6hdr {
  477. #define icmp6_addrconf_other icmp6_dataun.u_nd_ra.other
  478. #define icmp6_rt_lifetime icmp6_dataun.u_nd_ra.rt_lifetime
  479. #define icmp6_router_pref icmp6_dataun.u_nd_ra.router_pref
  480. -};
  481. +} __attribute__((packed, aligned(2)));
  482. #define ICMPV6_ROUTER_PREF_LOW 0x3
  483. --- a/include/net/ndisc.h
  484. +++ b/include/net/ndisc.h
  485. @@ -93,7 +93,7 @@ struct ra_msg {
  486. struct icmp6hdr icmph;
  487. __be32 reachable_time;
  488. __be32 retrans_timer;
  489. -};
  490. +} __attribute__((packed, aligned(2)));
  491. struct rd_msg {
  492. struct icmp6hdr icmph;
  493. @@ -372,10 +372,10 @@ static inline u32 ndisc_hashfn(const voi
  494. {
  495. const u32 *p32 = pkey;
  496. - return (((p32[0] ^ hash32_ptr(dev)) * hash_rnd[0]) +
  497. - (p32[1] * hash_rnd[1]) +
  498. - (p32[2] * hash_rnd[2]) +
  499. - (p32[3] * hash_rnd[3]));
  500. + return (((net_hdr_word(&p32[0]) ^ hash32_ptr(dev)) * hash_rnd[0]) +
  501. + (net_hdr_word(&p32[1]) * hash_rnd[1]) +
  502. + (net_hdr_word(&p32[2]) * hash_rnd[2]) +
  503. + (net_hdr_word(&p32[3]) * hash_rnd[3]));
  504. }
  505. static inline struct neighbour *__ipv6_neigh_lookup_noref(struct net_device *dev, const void *pkey)
  506. --- a/net/sched/cls_u32.c
  507. +++ b/net/sched/cls_u32.c
  508. @@ -155,7 +155,7 @@ next_knode:
  509. data = skb_header_pointer(skb, toff, 4, &hdata);
  510. if (!data)
  511. goto out;
  512. - if ((*data ^ key->val) & key->mask) {
  513. + if ((net_hdr_word(data) ^ key->val) & key->mask) {
  514. n = rcu_dereference_bh(n->next);
  515. goto next_knode;
  516. }
  517. @@ -206,8 +206,8 @@ check_terminal:
  518. &hdata);
  519. if (!data)
  520. goto out;
  521. - sel = ht->divisor & u32_hash_fold(*data, &n->sel,
  522. - n->fshift);
  523. + sel = ht->divisor & u32_hash_fold(net_hdr_word(data),
  524. + &n->sel, n->fshift);
  525. }
  526. if (!(n->sel.flags & (TC_U32_VAROFFSET | TC_U32_OFFSET | TC_U32_EAT)))
  527. goto next_ht;
  528. --- a/net/ipv6/ip6_offload.c
  529. +++ b/net/ipv6/ip6_offload.c
  530. @@ -259,7 +259,7 @@ INDIRECT_CALLABLE_SCOPE struct sk_buff *
  531. continue;
  532. iph2 = (struct ipv6hdr *)(p->data + off);
  533. - first_word = *(__be32 *)iph ^ *(__be32 *)iph2;
  534. + first_word = net_hdr_word(iph) ^ net_hdr_word(iph2);
  535. /* All fields must match except length and Traffic Class.
  536. * XXX skbs on the gro_list have all been parsed and pulled
  537. --- a/include/net/addrconf.h
  538. +++ b/include/net/addrconf.h
  539. @@ -52,7 +52,7 @@ struct prefix_info {
  540. __be32 reserved2;
  541. struct in6_addr prefix;
  542. -};
  543. +} __attribute__((packed, aligned(2)));
  544. /* rfc4861 4.6.2: IPv6 PIO is 32 bytes in size */
  545. static_assert(sizeof(struct prefix_info) == 32);
  546. --- a/include/net/inet_ecn.h
  547. +++ b/include/net/inet_ecn.h
  548. @@ -138,9 +138,9 @@ static inline int IP6_ECN_set_ce(struct
  549. if (INET_ECN_is_not_ect(ipv6_get_dsfield(iph)))
  550. return 0;
  551. - from = *(__be32 *)iph;
  552. + from = net_hdr_word(iph);
  553. to = from | htonl(INET_ECN_CE << 20);
  554. - *(__be32 *)iph = to;
  555. + net_hdr_word(iph) = to;
  556. if (skb->ip_summed == CHECKSUM_COMPLETE)
  557. skb->csum = csum_add(csum_sub(skb->csum, (__force __wsum)from),
  558. (__force __wsum)to);
  559. --- a/include/net/ipv6.h
  560. +++ b/include/net/ipv6.h
  561. @@ -149,7 +149,7 @@ struct frag_hdr {
  562. __u8 reserved;
  563. __be16 frag_off;
  564. __be32 identification;
  565. -};
  566. +} __attribute__((packed, aligned(2)));
  567. /*
  568. * Jumbo payload option, as described in RFC 2675 2.
  569. @@ -615,8 +615,8 @@ static inline void __ipv6_addr_set_half(
  570. }
  571. #endif
  572. #endif
  573. - addr[0] = wh;
  574. - addr[1] = wl;
  575. + net_hdr_word(&addr[0]) = wh;
  576. + net_hdr_word(&addr[1]) = wl;
  577. }
  578. static inline void ipv6_addr_set(struct in6_addr *addr,
  579. @@ -675,6 +675,8 @@ static inline bool ipv6_prefix_equal(con
  580. const __be32 *a1 = addr1->s6_addr32;
  581. const __be32 *a2 = addr2->s6_addr32;
  582. unsigned int pdw, pbi;
  583. + /* Used for last <32-bit fraction of prefix */
  584. + u32 pbia1, pbia2;
  585. /* check complete u32 in prefix */
  586. pdw = prefixlen >> 5;
  587. @@ -683,7 +685,9 @@ static inline bool ipv6_prefix_equal(con
  588. /* check incomplete u32 in prefix */
  589. pbi = prefixlen & 0x1f;
  590. - if (pbi && ((a1[pdw] ^ a2[pdw]) & htonl((0xffffffff) << (32 - pbi))))
  591. + pbia1 = net_hdr_word(&a1[pdw]);
  592. + pbia2 = net_hdr_word(&a2[pdw]);
  593. + if (pbi && ((pbia1 ^ pbia2) & htonl((0xffffffff) << (32 - pbi))))
  594. return false;
  595. return true;
  596. @@ -805,13 +809,13 @@ static inline void ipv6_addr_set_v4mappe
  597. */
  598. static inline int __ipv6_addr_diff32(const void *token1, const void *token2, int addrlen)
  599. {
  600. - const __be32 *a1 = token1, *a2 = token2;
  601. + const struct in6_addr *a1 = token1, *a2 = token2;
  602. int i;
  603. addrlen >>= 2;
  604. for (i = 0; i < addrlen; i++) {
  605. - __be32 xb = a1[i] ^ a2[i];
  606. + __be32 xb = a1->s6_addr32[i] ^ a2->s6_addr32[i];
  607. if (xb)
  608. return i * 32 + 31 - __fls(ntohl(xb));
  609. }
  610. @@ -1005,17 +1009,18 @@ static inline u32 ip6_multipath_hash_fie
  611. static inline void ip6_flow_hdr(struct ipv6hdr *hdr, unsigned int tclass,
  612. __be32 flowlabel)
  613. {
  614. - *(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | flowlabel;
  615. + net_hdr_word((__be32 *)hdr) =
  616. + htonl(0x60000000 | (tclass << 20)) | flowlabel;
  617. }
  618. static inline __be32 ip6_flowinfo(const struct ipv6hdr *hdr)
  619. {
  620. - return *(__be32 *)hdr & IPV6_FLOWINFO_MASK;
  621. + return net_hdr_word((__be32 *)hdr) & IPV6_FLOWINFO_MASK;
  622. }
  623. static inline __be32 ip6_flowlabel(const struct ipv6hdr *hdr)
  624. {
  625. - return *(__be32 *)hdr & IPV6_FLOWLABEL_MASK;
  626. + return net_hdr_word((__be32 *)hdr) & IPV6_FLOWLABEL_MASK;
  627. }
  628. static inline u8 ip6_tclass(__be32 flowinfo)
  629. --- a/include/net/secure_seq.h
  630. +++ b/include/net/secure_seq.h
  631. @@ -3,6 +3,7 @@
  632. #define _NET_SECURE_SEQ
  633. #include <linux/types.h>
  634. +#include <linux/in6.h>
  635. struct net;
  636. --- a/include/uapi/linux/in.h
  637. +++ b/include/uapi/linux/in.h
  638. @@ -91,7 +91,7 @@ enum {
  639. /* Internet address. */
  640. struct in_addr {
  641. __be32 s_addr;
  642. -};
  643. +} __attribute__((packed, aligned(2)));
  644. #endif
  645. #define IP_TOS 1
  646. --- a/net/ipv6/ip6_fib.c
  647. +++ b/net/ipv6/ip6_fib.c
  648. @@ -142,7 +142,7 @@ static __be32 addr_bit_set(const void *t
  649. * See include/asm-generic/bitops/le.h.
  650. */
  651. return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
  652. - addr[fn_bit >> 5];
  653. + net_hdr_word(&addr[fn_bit >> 5]);
  654. }
  655. struct fib6_info *fib6_info_alloc(gfp_t gfp_flags, bool with_fib6_nh)
  656. --- a/net/netfilter/nf_conntrack_proto_tcp.c
  657. +++ b/net/netfilter/nf_conntrack_proto_tcp.c
  658. @@ -406,7 +406,7 @@ static void tcp_sack(const struct sk_buf
  659. /* Fast path for timestamp-only option */
  660. if (length == TCPOLEN_TSTAMP_ALIGNED
  661. - && *(__be32 *)ptr == htonl((TCPOPT_NOP << 24)
  662. + && net_hdr_word(ptr) == htonl((TCPOPT_NOP << 24)
  663. | (TCPOPT_NOP << 16)
  664. | (TCPOPT_TIMESTAMP << 8)
  665. | TCPOLEN_TIMESTAMP))
  666. --- a/net/xfrm/xfrm_input.c
  667. +++ b/net/xfrm/xfrm_input.c
  668. @@ -168,8 +168,8 @@ int xfrm_parse_spi(struct sk_buff *skb,
  669. if (!pskb_may_pull(skb, hlen))
  670. return -EINVAL;
  671. - *spi = *(__be32 *)(skb_transport_header(skb) + offset);
  672. - *seq = *(__be32 *)(skb_transport_header(skb) + offset_seq);
  673. + *spi = net_hdr_word(skb_transport_header(skb) + offset);
  674. + *seq = net_hdr_word(skb_transport_header(skb) + offset_seq);
  675. return 0;
  676. }
  677. EXPORT_SYMBOL(xfrm_parse_spi);
  678. --- a/net/ipv4/tcp_input.c
  679. +++ b/net/ipv4/tcp_input.c
  680. @@ -4183,14 +4183,16 @@ static bool tcp_parse_aligned_timestamp(
  681. {
  682. const __be32 *ptr = (const __be32 *)(th + 1);
  683. - if (*ptr == htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16)
  684. - | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
  685. + if (net_hdr_word(ptr) ==
  686. + htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
  687. + (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP)) {
  688. tp->rx_opt.saw_tstamp = 1;
  689. ++ptr;
  690. - tp->rx_opt.rcv_tsval = ntohl(*ptr);
  691. + tp->rx_opt.rcv_tsval = get_unaligned_be32(ptr);
  692. ++ptr;
  693. - if (*ptr)
  694. - tp->rx_opt.rcv_tsecr = ntohl(*ptr) - tp->tsoffset;
  695. + if (net_hdr_word(ptr))
  696. + tp->rx_opt.rcv_tsecr = get_unaligned_be32(ptr) -
  697. + tp->tsoffset;
  698. else
  699. tp->rx_opt.rcv_tsecr = 0;
  700. return true;
  701. --- a/include/uapi/linux/if_pppox.h
  702. +++ b/include/uapi/linux/if_pppox.h
  703. @@ -51,6 +51,7 @@ struct pppoe_addr {
  704. */
  705. struct pptp_addr {
  706. __u16 call_id;
  707. + __u16 pad;
  708. struct in_addr sin_addr;
  709. };
  710. --- a/include/net/neighbour.h
  711. +++ b/include/net/neighbour.h
  712. @@ -286,8 +286,10 @@ static inline bool neigh_key_eq128(const
  713. const u32 *n32 = (const u32 *)n->primary_key;
  714. const u32 *p32 = pkey;
  715. - return ((n32[0] ^ p32[0]) | (n32[1] ^ p32[1]) |
  716. - (n32[2] ^ p32[2]) | (n32[3] ^ p32[3])) == 0;
  717. + return ((n32[0] ^ net_hdr_word(&p32[0])) |
  718. + (n32[1] ^ net_hdr_word(&p32[1])) |
  719. + (n32[2] ^ net_hdr_word(&p32[2])) |
  720. + (n32[3] ^ net_hdr_word(&p32[3]))) == 0;
  721. }
  722. static inline struct neighbour *___neigh_lookup_noref(
  723. --- a/include/uapi/linux/netfilter_arp/arp_tables.h
  724. +++ b/include/uapi/linux/netfilter_arp/arp_tables.h
  725. @@ -70,7 +70,7 @@ struct arpt_arp {
  726. __u8 flags;
  727. /* Inverse flags */
  728. __u16 invflags;
  729. -};
  730. +} __attribute__((aligned(4)));
  731. /* Values for "flag" field in struct arpt_ip (general arp structure).
  732. * No flags defined yet.
  733. --- a/net/core/utils.c
  734. +++ b/net/core/utils.c
  735. @@ -460,8 +460,14 @@ void inet_proto_csum_replace16(__sum16 *
  736. bool pseudohdr)
  737. {
  738. __be32 diff[] = {
  739. - ~from[0], ~from[1], ~from[2], ~from[3],
  740. - to[0], to[1], to[2], to[3],
  741. + ~net_hdr_word(&from[0]),
  742. + ~net_hdr_word(&from[1]),
  743. + ~net_hdr_word(&from[2]),
  744. + ~net_hdr_word(&from[3]),
  745. + net_hdr_word(&to[0]),
  746. + net_hdr_word(&to[1]),
  747. + net_hdr_word(&to[2]),
  748. + net_hdr_word(&to[3]),
  749. };
  750. if (skb->ip_summed != CHECKSUM_PARTIAL) {
  751. *sum = csum_fold(csum_partial(diff, sizeof(diff),
  752. --- a/include/linux/etherdevice.h
  753. +++ b/include/linux/etherdevice.h
  754. @@ -555,7 +555,7 @@ static inline bool is_etherdev_addr(cons
  755. * @b: Pointer to Ethernet header
  756. *
  757. * Compare two Ethernet headers, returns 0 if equal.
  758. - * This assumes that the network header (i.e., IP header) is 4-byte
  759. + * This assumes that the network header (i.e., IP header) is 2-byte
  760. * aligned OR the platform can handle unaligned access. This is the
  761. * case for all packets coming into netif_receive_skb or similar
  762. * entry points.
  763. @@ -578,11 +578,12 @@ static inline unsigned long compare_ethe
  764. fold |= *(unsigned long *)(a + 6) ^ *(unsigned long *)(b + 6);
  765. return fold;
  766. #else
  767. - u32 *a32 = (u32 *)((u8 *)a + 2);
  768. - u32 *b32 = (u32 *)((u8 *)b + 2);
  769. + const u16 *a16 = a;
  770. + const u16 *b16 = b;
  771. - return (*(u16 *)a ^ *(u16 *)b) | (a32[0] ^ b32[0]) |
  772. - (a32[1] ^ b32[1]) | (a32[2] ^ b32[2]);
  773. + return (a16[0] ^ b16[0]) | (a16[1] ^ b16[1]) | (a16[2] ^ b16[2]) |
  774. + (a16[3] ^ b16[3]) | (a16[4] ^ b16[4]) | (a16[5] ^ b16[5]) |
  775. + (a16[6] ^ b16[6]);
  776. #endif
  777. }
  778. --- a/net/ipv4/tcp_offload.c
  779. +++ b/net/ipv4/tcp_offload.c
  780. @@ -220,7 +220,7 @@ struct sk_buff *tcp_gro_receive(struct l
  781. th2 = tcp_hdr(p);
  782. - if (*(u32 *)&th->source ^ *(u32 *)&th2->source) {
  783. + if (net_hdr_word(&th->source) ^ net_hdr_word(&th2->source)) {
  784. NAPI_GRO_CB(p)->same_flow = 0;
  785. continue;
  786. }
  787. @@ -238,8 +238,8 @@ found:
  788. ~(TCP_FLAG_CWR | TCP_FLAG_FIN | TCP_FLAG_PSH));
  789. flush |= (__force int)(th->ack_seq ^ th2->ack_seq);
  790. for (i = sizeof(*th); i < thlen; i += 4)
  791. - flush |= *(u32 *)((u8 *)th + i) ^
  792. - *(u32 *)((u8 *)th2 + i);
  793. + flush |= net_hdr_word((u8 *)th + i) ^
  794. + net_hdr_word((u8 *)th2 + i);
  795. /* When we receive our second frame we can made a decision on if we
  796. * continue this flow as an atomic flow with a fixed ID or if we use
  797. --- a/net/ipv6/netfilter/ip6table_mangle.c
  798. +++ b/net/ipv6/netfilter/ip6table_mangle.c
  799. @@ -44,7 +44,7 @@ ip6t_mangle_out(void *priv, struct sk_bu
  800. hop_limit = ipv6_hdr(skb)->hop_limit;
  801. /* flowlabel and prio (includes version, which shouldn't change either */
  802. - flowlabel = *((u_int32_t *)ipv6_hdr(skb));
  803. + flowlabel = net_hdr_word(ipv6_hdr(skb));
  804. ret = ip6t_do_table(priv, skb, state);
  805. @@ -53,7 +53,7 @@ ip6t_mangle_out(void *priv, struct sk_bu
  806. !ipv6_addr_equal(&ipv6_hdr(skb)->daddr, &daddr) ||
  807. skb->mark != mark ||
  808. ipv6_hdr(skb)->hop_limit != hop_limit ||
  809. - flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
  810. + flowlabel != net_hdr_word(ipv6_hdr(skb)))) {
  811. err = ip6_route_me_harder(state->net, state->sk, skb);
  812. if (err < 0)
  813. ret = NF_DROP_ERR(err);