Skip to content

Commit 2a50f28

Browse files
Al ViroDavid S. Miller
authored andcommitted
[ATALK]: endianness annotations
Signed-off-by: Al Viro <viro@zeniv.linux.org.uk> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 82fe7c9 commit 2a50f28

3 files changed

Lines changed: 30 additions & 94 deletions

File tree

‎drivers/net/appletalk/ipddp.c‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
145145

146146
/* Create the Extended DDP header */
147147
ddp = (struct ddpehdr *)skb->data;
148-
ddp->deh_len = skb->len;
149-
ddp->deh_hops = 1;
150-
ddp->deh_pad = 0;
148+
ddp->deh_len_hops = htons(skb->len + (1<<10));
151149
ddp->deh_sum = 0;
152150

153151
/*
@@ -170,7 +168,6 @@ static int ipddp_xmit(struct sk_buff *skb, struct net_device *dev)
170168
ddp->deh_sport = 72;
171169

172170
*((__u8 *)(ddp+1)) = 22; /* ddp type = IP */
173-
*((__u16 *)ddp)=ntohs(*((__u16 *)ddp)); /* fix up length field */
174171

175172
skb->protocol = htons(ETH_P_ATALK); /* Protocol has changed */
176173

‎include/linux/atalk.h‎

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -88,15 +88,7 @@ static inline struct atalk_sock *at_sk(struct sock *sk)
8888
#include <asm/byteorder.h>
8989

9090
struct ddpehdr {
91-
#ifdef __LITTLE_ENDIAN_BITFIELD
92-
__u16 deh_len:10,
93-
deh_hops:4,
94-
deh_pad:2;
95-
#else
96-
__u16 deh_pad:2,
97-
deh_hops:4,
98-
deh_len:10;
99-
#endif
91+
__be16 deh_len_hops; /* lower 10 bits are length, next 4 - hops */
10092
__be16 deh_sum;
10193
__be16 deh_dnet;
10294
__be16 deh_snet;
@@ -112,36 +104,6 @@ static __inline__ struct ddpehdr *ddp_hdr(struct sk_buff *skb)
112104
return (struct ddpehdr *)skb->h.raw;
113105
}
114106

115-
/*
116-
* Don't drop the struct into the struct above. You'll get some
117-
* surprise padding.
118-
*/
119-
struct ddpebits {
120-
#ifdef __LITTLE_ENDIAN_BITFIELD
121-
__u16 deh_len:10,
122-
deh_hops:4,
123-
deh_pad:2;
124-
#else
125-
__u16 deh_pad:2,
126-
deh_hops:4,
127-
deh_len:10;
128-
#endif
129-
};
130-
131-
/* Short form header */
132-
struct ddpshdr {
133-
#ifdef __LITTLE_ENDIAN_BITFIELD
134-
__u16 dsh_len:10,
135-
dsh_pad:6;
136-
#else
137-
__u16 dsh_pad:6,
138-
dsh_len:10;
139-
#endif
140-
__u8 dsh_dport;
141-
__u8 dsh_sport;
142-
/* And netatalk apps expect to stick the type in themselves */
143-
};
144-
145107
/* AppleTalk AARP headers */
146108
struct elapaarp {
147109
__be16 hw_type;

‎net/appletalk/ddp.c‎

Lines changed: 28 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,15 +1002,15 @@ static unsigned long atalk_sum_skb(const struct sk_buff *skb, int offset,
10021002
return sum;
10031003
}
10041004

1005-
static unsigned short atalk_checksum(const struct sk_buff *skb, int len)
1005+
static __be16 atalk_checksum(const struct sk_buff *skb, int len)
10061006
{
10071007
unsigned long sum;
10081008

10091009
/* skip header 4 bytes */
10101010
sum = atalk_sum_skb(skb, 4, len-4, 0);
10111011

10121012
/* Use 0xFFFF for 0. 0 itself means none */
1013-
return sum ? htons((unsigned short)sum) : 0xFFFF;
1013+
return sum ? htons((unsigned short)sum) : htons(0xFFFF);
10141014
}
10151015

10161016
static struct proto ddp_proto = {
@@ -1289,7 +1289,7 @@ static int handle_ip_over_ddp(struct sk_buff *skb)
12891289
#endif
12901290

12911291
static void atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
1292-
struct ddpehdr *ddp, struct ddpebits *ddphv,
1292+
struct ddpehdr *ddp, __u16 len_hops,
12931293
int origlen)
12941294
{
12951295
struct atalk_route *rt;
@@ -1317,10 +1317,12 @@ static void atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
13171317

13181318
/* Route the packet */
13191319
rt = atrtr_find(&ta);
1320-
if (!rt || ddphv->deh_hops == DDP_MAXHOPS)
1320+
/* increment hops count */
1321+
len_hops += 1 << 10;
1322+
if (!rt || !(len_hops & (15 << 10)))
13211323
goto free_it;
1324+
13221325
/* FIXME: use skb->cb to be able to use shared skbs */
1323-
ddphv->deh_hops++;
13241326

13251327
/*
13261328
* Route goes through another gateway, so set the target to the
@@ -1335,11 +1337,10 @@ static void atalk_route_packet(struct sk_buff *skb, struct net_device *dev,
13351337
/* Fix up skb->len field */
13361338
skb_trim(skb, min_t(unsigned int, origlen,
13371339
(rt->dev->hard_header_len +
1338-
ddp_dl->header_length + ddphv->deh_len)));
1340+
ddp_dl->header_length + (len_hops & 1023))));
13391341

1340-
/* Mend the byte order */
13411342
/* FIXME: use skb->cb to be able to use shared skbs */
1342-
*((__u16 *)ddp) = ntohs(*((__u16 *)ddphv));
1343+
ddp->deh_len_hops = htons(len_hops);
13431344

13441345
/*
13451346
* Send the buffer onwards
@@ -1394,7 +1395,7 @@ static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
13941395
struct atalk_iface *atif;
13951396
struct sockaddr_at tosat;
13961397
int origlen;
1397-
struct ddpebits ddphv;
1398+
__u16 len_hops;
13981399

13991400
/* Don't mangle buffer if shared */
14001401
if (!(skb = skb_share_check(skb, GFP_ATOMIC)))
@@ -1406,16 +1407,11 @@ static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
14061407

14071408
ddp = ddp_hdr(skb);
14081409

1409-
/*
1410-
* Fix up the length field [Ok this is horrible but otherwise
1411-
* I end up with unions of bit fields and messy bit field order
1412-
* compiler/endian dependencies..]
1413-
*/
1414-
*((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1410+
len_hops = ntohs(ddp->deh_len_hops);
14151411

14161412
/* Trim buffer in case of stray trailing data */
14171413
origlen = skb->len;
1418-
skb_trim(skb, min_t(unsigned int, skb->len, ddphv.deh_len));
1414+
skb_trim(skb, min_t(unsigned int, skb->len, len_hops & 1023));
14191415

14201416
/*
14211417
* Size check to see if ddp->deh_len was crap
@@ -1430,7 +1426,7 @@ static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
14301426
* valid for net byte orders all over the networking code...
14311427
*/
14321428
if (ddp->deh_sum &&
1433-
atalk_checksum(skb, ddphv.deh_len) != ddp->deh_sum)
1429+
atalk_checksum(skb, len_hops & 1023) != ddp->deh_sum)
14341430
/* Not a valid AppleTalk frame - dustbin time */
14351431
goto freeit;
14361432

@@ -1444,7 +1440,7 @@ static int atalk_rcv(struct sk_buff *skb, struct net_device *dev,
14441440
/* Not ours, so we route the packet via the correct
14451441
* AppleTalk iface
14461442
*/
1447-
atalk_route_packet(skb, dev, ddp, &ddphv, origlen);
1443+
atalk_route_packet(skb, dev, ddp, len_hops, origlen);
14481444
goto out;
14491445
}
14501446

@@ -1489,7 +1485,7 @@ static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
14891485
/* Find our address */
14901486
struct atalk_addr *ap = atalk_find_dev_addr(dev);
14911487

1492-
if (!ap || skb->len < sizeof(struct ddpshdr))
1488+
if (!ap || skb->len < sizeof(__be16) || skb->len > 1023)
14931489
goto freeit;
14941490

14951491
/* Don't mangle buffer if shared */
@@ -1519,11 +1515,8 @@ static int ltalk_rcv(struct sk_buff *skb, struct net_device *dev,
15191515
/*
15201516
* Not sure about this bit...
15211517
*/
1522-
ddp->deh_len = skb->len;
1523-
ddp->deh_hops = DDP_MAXHOPS; /* Non routable, so force a drop
1524-
if we slip up later */
1525-
/* Mend the byte order */
1526-
*((__u16 *)ddp) = htons(*((__u16 *)ddp));
1518+
/* Non routable, so force a drop if we slip up later */
1519+
ddp->deh_len_hops = htons(skb->len + (DDP_MAXHOPS << 10));
15271520
}
15281521
skb->h.raw = skb->data;
15291522

@@ -1622,16 +1615,7 @@ static int atalk_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr
16221615
SOCK_DEBUG(sk, "SK %p: Begin build.\n", sk);
16231616

16241617
ddp = (struct ddpehdr *)skb_put(skb, sizeof(struct ddpehdr));
1625-
ddp->deh_pad = 0;
1626-
ddp->deh_hops = 0;
1627-
ddp->deh_len = len + sizeof(*ddp);
1628-
/*
1629-
* Fix up the length field [Ok this is horrible but otherwise
1630-
* I end up with unions of bit fields and messy bit field order
1631-
* compiler/endian dependencies..
1632-
*/
1633-
*((__u16 *)ddp) = ntohs(*((__u16 *)ddp));
1634-
1618+
ddp->deh_len_hops = htons(len + sizeof(*ddp));
16351619
ddp->deh_dnet = usat->sat_addr.s_net;
16361620
ddp->deh_snet = at->src_net;
16371621
ddp->deh_dnode = usat->sat_addr.s_node;
@@ -1712,34 +1696,27 @@ static int atalk_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr
17121696
struct sockaddr_at *sat = (struct sockaddr_at *)msg->msg_name;
17131697
struct ddpehdr *ddp;
17141698
int copied = 0;
1699+
int offset = 0;
17151700
int err = 0;
1716-
struct ddpebits ddphv;
17171701
struct sk_buff *skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
17181702
flags & MSG_DONTWAIT, &err);
17191703
if (!skb)
17201704
return err;
17211705

17221706
/* FIXME: use skb->cb to be able to use shared skbs */
17231707
ddp = ddp_hdr(skb);
1724-
*((__u16 *)&ddphv) = ntohs(*((__u16 *)ddp));
1708+
copied = ntohs(ddp->deh_len_hops) & 1023;
17251709

1726-
if (sk->sk_type == SOCK_RAW) {
1727-
copied = ddphv.deh_len;
1728-
if (copied > size) {
1729-
copied = size;
1730-
msg->msg_flags |= MSG_TRUNC;
1731-
}
1710+
if (sk->sk_type != SOCK_RAW) {
1711+
offset = sizeof(*ddp);
1712+
copied -= offset;
1713+
}
17321714

1733-
err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
1734-
} else {
1735-
copied = ddphv.deh_len - sizeof(*ddp);
1736-
if (copied > size) {
1737-
copied = size;
1738-
msg->msg_flags |= MSG_TRUNC;
1739-
}
1740-
err = skb_copy_datagram_iovec(skb, sizeof(*ddp),
1741-
msg->msg_iov, copied);
1715+
if (copied > size) {
1716+
copied = size;
1717+
msg->msg_flags |= MSG_TRUNC;
17421718
}
1719+
err = skb_copy_datagram_iovec(skb, offset, msg->msg_iov, copied);
17431720

17441721
if (!err) {
17451722
if (sat) {

0 commit comments

Comments
 (0)