aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOliver Hartkopp <socketcan@hartkopp.net>2016-06-17 09:35:26 -0400
committerMarc Kleine-Budde <mkl@pengutronix.de>2016-06-17 09:39:46 -0400
commit2b5f5f5dc114219dcd848fb0ff344acb413c11ef (patch)
tree7cbe877f2321b6873525640171f42417fb9e7615
parent72c8a89ad2e4de18849674f30589baa5ebb4fbc1 (diff)
can: bcm: unify bcm_msg_head handling and prepare function parameters
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net> Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
-rw-r--r--net/can/bcm.c54
1 files changed, 29 insertions, 25 deletions
diff --git a/net/can/bcm.c b/net/can/bcm.c
index 83aa6cf61de9..f3bf3875313a 100644
--- a/net/can/bcm.c
+++ b/net/can/bcm.c
@@ -693,13 +693,13 @@ rx_starttimer:
693/* 693/*
694 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements 694 * helpers for bcm_op handling: find & delete bcm [rx|tx] op elements
695 */ 695 */
696static struct bcm_op *bcm_find_op(struct list_head *ops, canid_t can_id, 696static struct bcm_op *bcm_find_op(struct list_head *ops,
697 int ifindex) 697 struct bcm_msg_head *mh, int ifindex)
698{ 698{
699 struct bcm_op *op; 699 struct bcm_op *op;
700 700
701 list_for_each_entry(op, ops, list) { 701 list_for_each_entry(op, ops, list) {
702 if ((op->can_id == can_id) && (op->ifindex == ifindex)) 702 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex))
703 return op; 703 return op;
704 } 704 }
705 705
@@ -742,12 +742,13 @@ static void bcm_rx_unreg(struct net_device *dev, struct bcm_op *op)
742/* 742/*
743 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops) 743 * bcm_delete_rx_op - find and remove a rx op (returns number of removed ops)
744 */ 744 */
745static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex) 745static int bcm_delete_rx_op(struct list_head *ops, struct bcm_msg_head *mh,
746 int ifindex)
746{ 747{
747 struct bcm_op *op, *n; 748 struct bcm_op *op, *n;
748 749
749 list_for_each_entry_safe(op, n, ops, list) { 750 list_for_each_entry_safe(op, n, ops, list) {
750 if ((op->can_id == can_id) && (op->ifindex == ifindex)) { 751 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex)) {
751 752
752 /* 753 /*
753 * Don't care if we're bound or not (due to netdev 754 * Don't care if we're bound or not (due to netdev
@@ -787,12 +788,13 @@ static int bcm_delete_rx_op(struct list_head *ops, canid_t can_id, int ifindex)
787/* 788/*
788 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops) 789 * bcm_delete_tx_op - find and remove a tx op (returns number of removed ops)
789 */ 790 */
790static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex) 791static int bcm_delete_tx_op(struct list_head *ops, struct bcm_msg_head *mh,
792 int ifindex)
791{ 793{
792 struct bcm_op *op, *n; 794 struct bcm_op *op, *n;
793 795
794 list_for_each_entry_safe(op, n, ops, list) { 796 list_for_each_entry_safe(op, n, ops, list) {
795 if ((op->can_id == can_id) && (op->ifindex == ifindex)) { 797 if ((op->can_id == mh->can_id) && (op->ifindex == ifindex)) {
796 list_del(&op->list); 798 list_del(&op->list);
797 bcm_remove_op(op); 799 bcm_remove_op(op);
798 return 1; /* done */ 800 return 1; /* done */
@@ -808,7 +810,7 @@ static int bcm_delete_tx_op(struct list_head *ops, canid_t can_id, int ifindex)
808static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head, 810static int bcm_read_op(struct list_head *ops, struct bcm_msg_head *msg_head,
809 int ifindex) 811 int ifindex)
810{ 812{
811 struct bcm_op *op = bcm_find_op(ops, msg_head->can_id, ifindex); 813 struct bcm_op *op = bcm_find_op(ops, msg_head, ifindex);
812 814
813 if (!op) 815 if (!op)
814 return -EINVAL; 816 return -EINVAL;
@@ -845,8 +847,7 @@ static int bcm_tx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
845 return -EINVAL; 847 return -EINVAL;
846 848
847 /* check the given can_id */ 849 /* check the given can_id */
848 op = bcm_find_op(&bo->tx_ops, msg_head->can_id, ifindex); 850 op = bcm_find_op(&bo->tx_ops, msg_head, ifindex);
849
850 if (op) { 851 if (op) {
851 /* update existing BCM operation */ 852 /* update existing BCM operation */
852 853
@@ -1010,7 +1011,7 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1010 return -EINVAL; 1011 return -EINVAL;
1011 1012
1012 /* check the given can_id */ 1013 /* check the given can_id */
1013 op = bcm_find_op(&bo->rx_ops, msg_head->can_id, ifindex); 1014 op = bcm_find_op(&bo->rx_ops, msg_head, ifindex);
1014 if (op) { 1015 if (op) {
1015 /* update existing BCM operation */ 1016 /* update existing BCM operation */
1016 1017
@@ -1192,7 +1193,8 @@ static int bcm_rx_setup(struct bcm_msg_head *msg_head, struct msghdr *msg,
1192/* 1193/*
1193 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg) 1194 * bcm_tx_send - send a single CAN frame to the CAN interface (for bcm_sendmsg)
1194 */ 1195 */
1195static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk) 1196static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk,
1197 int cfsiz)
1196{ 1198{
1197 struct sk_buff *skb; 1199 struct sk_buff *skb;
1198 struct net_device *dev; 1200 struct net_device *dev;
@@ -1202,13 +1204,13 @@ static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1202 if (!ifindex) 1204 if (!ifindex)
1203 return -ENODEV; 1205 return -ENODEV;
1204 1206
1205 skb = alloc_skb(CFSIZ + sizeof(struct can_skb_priv), GFP_KERNEL); 1207 skb = alloc_skb(cfsiz + sizeof(struct can_skb_priv), GFP_KERNEL);
1206 if (!skb) 1208 if (!skb)
1207 return -ENOMEM; 1209 return -ENOMEM;
1208 1210
1209 can_skb_reserve(skb); 1211 can_skb_reserve(skb);
1210 1212
1211 err = memcpy_from_msg(skb_put(skb, CFSIZ), msg, CFSIZ); 1213 err = memcpy_from_msg(skb_put(skb, cfsiz), msg, cfsiz);
1212 if (err < 0) { 1214 if (err < 0) {
1213 kfree_skb(skb); 1215 kfree_skb(skb);
1214 return err; 1216 return err;
@@ -1230,7 +1232,7 @@ static int bcm_tx_send(struct msghdr *msg, int ifindex, struct sock *sk)
1230 if (err) 1232 if (err)
1231 return err; 1233 return err;
1232 1234
1233 return CFSIZ + MHSIZ; 1235 return cfsiz + MHSIZ;
1234} 1236}
1235 1237
1236/* 1238/*
@@ -1248,7 +1250,15 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1248 return -ENOTCONN; 1250 return -ENOTCONN;
1249 1251
1250 /* check for valid message length from userspace */ 1252 /* check for valid message length from userspace */
1251 if (size < MHSIZ || (size - MHSIZ) % CFSIZ) 1253 if (size < MHSIZ)
1254 return -EINVAL;
1255
1256 /* read message head information */
1257 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
1258 if (ret < 0)
1259 return ret;
1260
1261 if ((size - MHSIZ) % CFSIZ)
1252 return -EINVAL; 1262 return -EINVAL;
1253 1263
1254 /* check for alternative ifindex for this bcm_op */ 1264 /* check for alternative ifindex for this bcm_op */
@@ -1282,12 +1292,6 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1282 } 1292 }
1283 } 1293 }
1284 1294
1285 /* read message head information */
1286
1287 ret = memcpy_from_msg((u8 *)&msg_head, msg, MHSIZ);
1288 if (ret < 0)
1289 return ret;
1290
1291 lock_sock(sk); 1295 lock_sock(sk);
1292 1296
1293 switch (msg_head.opcode) { 1297 switch (msg_head.opcode) {
@@ -1301,14 +1305,14 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1301 break; 1305 break;
1302 1306
1303 case TX_DELETE: 1307 case TX_DELETE:
1304 if (bcm_delete_tx_op(&bo->tx_ops, msg_head.can_id, ifindex)) 1308 if (bcm_delete_tx_op(&bo->tx_ops, &msg_head, ifindex))
1305 ret = MHSIZ; 1309 ret = MHSIZ;
1306 else 1310 else
1307 ret = -EINVAL; 1311 ret = -EINVAL;
1308 break; 1312 break;
1309 1313
1310 case RX_DELETE: 1314 case RX_DELETE:
1311 if (bcm_delete_rx_op(&bo->rx_ops, msg_head.can_id, ifindex)) 1315 if (bcm_delete_rx_op(&bo->rx_ops, &msg_head, ifindex))
1312 ret = MHSIZ; 1316 ret = MHSIZ;
1313 else 1317 else
1314 ret = -EINVAL; 1318 ret = -EINVAL;
@@ -1331,7 +1335,7 @@ static int bcm_sendmsg(struct socket *sock, struct msghdr *msg, size_t size)
1331 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ)) 1335 if ((msg_head.nframes != 1) || (size != CFSIZ + MHSIZ))
1332 ret = -EINVAL; 1336 ret = -EINVAL;
1333 else 1337 else
1334 ret = bcm_tx_send(msg, ifindex, sk); 1338 ret = bcm_tx_send(msg, ifindex, sk, CFSIZ);
1335 break; 1339 break;
1336 1340
1337 default: 1341 default: